diff --git a/src/node/module.ts b/src/node/module.ts index 78ee2840481..c645a7e3c35 100644 --- a/src/node/module.ts +++ b/src/node/module.ts @@ -3,7 +3,10 @@ // https://opensource.org/licenses/Apache-2.0 import { default as moduleUtil } from 'node-internal:module'; -import { ERR_INVALID_ARG_VALUE } from 'node-internal:internal_errors'; +import { + ERR_INVALID_ARG_VALUE, + ERR_METHOD_NOT_IMPLEMENTED, +} from 'node-internal:internal_errors'; export function createRequire( path: string | URL @@ -21,16 +24,23 @@ export function createRequire( ); } - return moduleUtil.createRequire(normalizedPath); + // TODO(soon): We should move this to C++ land. + // Ref: https://nodejs.org/docs/latest/api/modules.html#requireid + return Object.assign(moduleUtil.createRequire(normalizedPath), { + // We don't throw ERR_METHOD_NOT_IMPLEMENTED because it's too disruptive. + resolve(): void { + return undefined; + }, + cache: Object.create(null), + main: undefined, + }); } // Indicates only that the given specifier is known to be a // Node.js built-in module specifier with or with the the // 'node:' prefix. A true return value does not guarantee that // the module is actually implemented in the runtime. -export function isBuiltin(specifier: string): boolean { - return moduleUtil.isBuiltin(specifier); -} +export const isBuiltin = moduleUtil.isBuiltin.bind(moduleUtil); // Intentionally does not include modules with mandatory 'node:' // prefix like `node:test`. @@ -110,8 +120,42 @@ export const builtinModules = [ ]; Object.freeze(builtinModules); +export function findSourceMap(): void { + // Not meaningful to implement in the context of workerd. + throw new ERR_METHOD_NOT_IMPLEMENTED('module.findSourceMap'); +} + +export function register(): void { + // Not meaningful to implement in the context of workerd. + throw new ERR_METHOD_NOT_IMPLEMENTED('module.register'); +} + +export function syncBuiltinESMExports(): void { + // Not meaningful to implement in the context of workerd. + throw new ERR_METHOD_NOT_IMPLEMENTED('module.syncBuiltinESMExports'); +} + +// +// IMPORTANT NOTE! +// +// We are deliberately not including any of these functions below because +// they are either experimental, in active development or not relevant +// as of January 2025. +// +// - module.registerHooks() +// - module.stripTypeScriptTypes() +// - module.SourceMap +// - module.constants.compileCacheStatus +// - module.enableCompileCache() +// - module.flushCompileCache() +// - module.getCompileCacheDir() +// + export default { createRequire, isBuiltin, builtinModules, + findSourceMap, + register, + syncBuiltinESMExports, }; diff --git a/src/workerd/api/node/BUILD.bazel b/src/workerd/api/node/BUILD.bazel index c1349c203ad..2fca86feb09 100644 --- a/src/workerd/api/node/BUILD.bazel +++ b/src/workerd/api/node/BUILD.bazel @@ -212,9 +212,9 @@ wd_test( ) wd_test( - src = "tests/module-create-require-test.wd-test", + src = "tests/module-nodejs-test.wd-test", args = ["--experimental"], - data = ["tests/module-create-require-test.js"], + data = ["tests/module-nodejs-test.js"], ) wd_test( diff --git a/src/workerd/api/node/tests/module-create-require-test.js b/src/workerd/api/node/tests/module-nodejs-test.js similarity index 88% rename from src/workerd/api/node/tests/module-create-require-test.js rename to src/workerd/api/node/tests/module-nodejs-test.js index 724a48465c0..ade02bc0085 100644 --- a/src/workerd/api/node/tests/module-create-require-test.js +++ b/src/workerd/api/node/tests/module-nodejs-test.js @@ -80,3 +80,17 @@ export const isBuiltinTest = { }); }, }; + +export const testErrorMethodNotImplemented = { + async test() { + const m = await import('node:module'); + const methods = ['findSourceMap', 'register', 'syncBuiltinESMExports']; + + for (const method of methods) { + throws(() => m[method](), { + name: 'Error', + code: 'ERR_METHOD_NOT_IMPLEMENTED', + }); + } + }, +}; diff --git a/src/workerd/api/node/tests/module-create-require-test.wd-test b/src/workerd/api/node/tests/module-nodejs-test.wd-test similarity index 86% rename from src/workerd/api/node/tests/module-create-require-test.wd-test rename to src/workerd/api/node/tests/module-nodejs-test.wd-test index c550ff58d5e..c56c3f37867 100644 --- a/src/workerd/api/node/tests/module-create-require-test.wd-test +++ b/src/workerd/api/node/tests/module-nodejs-test.wd-test @@ -2,10 +2,10 @@ using Workerd = import "/workerd/workerd.capnp"; const unitTests :Workerd.Config = ( services = [ - ( name = "module-create-require-test", + ( name = "nodejs-module-test", worker = ( modules = [ - (name = "worker", esModule = embed "module-create-require-test.js"), + (name = "worker", esModule = embed "module-nodejs-test.js"), (name = "foo", esModule = "export default 1;"), (name = "bar", esModule = "export default 2; export const __cjsUnwrapDefault = true;"), (name = "baz", commonJsModule = "module.exports = 3;"),