diff --git a/doc/api/module.md b/doc/api/module.md index f26590d43b55f0..42336f7120bf9e 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -28,7 +28,7 @@ added: A list of the names of all modules provided by Node.js. Can be used to verify if a module is maintained by a third party or not. -Note: the list doesn't contain [prefix-only modules][] like `node:test`. +Note: the list also contains [prefix-only modules][] like `node:test`. `module` in this context isn't the same object that's provided by the [module wrapper][]. To access it, require the `Module` module: diff --git a/doc/api/modules.md b/doc/api/modules.md index 9daaf5111deb0a..1c8ae41e25629d 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -513,7 +513,7 @@ Some built-in modules are always preferentially loaded if their identifier is passed to `require()`. For instance, `require('http')` will always return the built-in HTTP module, even if there is a file by that name. The list of built-in modules that can be loaded without using the `node:` prefix is exposed -as [`module.builtinModules`][]. +in [`module.builtinModules`][], listed without the prefix. ### Built-in modules with mandatory `node:` prefix @@ -527,6 +527,8 @@ taken the name. Currently the built-in modules that requires the `node:` prefix * [`node:test`][] * [`node:test/reporters`][] +The list of these modules is exposed in [`module.builtinModules`][], including the prefix. + ## Cycles diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index c11f70dd6bf329..8c81fe2f742915 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -320,14 +320,17 @@ class BuiltinModule { ); } - static getCanBeRequiredByUsersWithoutSchemeList() { - return ArrayFrom(canBeRequiredByUsersWithoutSchemeList); - } - static getSchemeOnlyModuleNames() { return ArrayFrom(schemelessBlockList); } + static getAllBuiltinModuleIds() { + return [ + ...canBeRequiredByUsersWithoutSchemeList, + ...ArrayFrom(schemelessBlockList, (x) => `node:${x}`), + ]; + } + // Used by user-land module loaders to compile and load builtins. compileForPublicLoader() { if (!BuiltinModule.canBeRequiredByUsers(this.id)) { diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 1aadb45f936d6c..905a1a26ef35f0 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -434,8 +434,8 @@ Module.isBuiltin = BuiltinModule.isBuiltin; */ function initializeCJS() { // This need to be done at runtime in case --expose-internals is set. - const builtinModules = BuiltinModule.getCanBeRequiredByUsersWithoutSchemeList(); - Module.builtinModules = ObjectFreeze(builtinModules); + + Module.builtinModules = ObjectFreeze(BuiltinModule.getAllBuiltinModuleIds()); initializeCjsConditions(); diff --git a/test/parallel/test-internal-module-require.js b/test/parallel/test-internal-module-require.js index c6e2057d3da1ee..058273c7ea4304 100644 --- a/test/parallel/test-internal-module-require.js +++ b/test/parallel/test-internal-module-require.js @@ -87,6 +87,9 @@ if (process.argv[2] === 'child') { }); } else { require(id); + if (!id.startsWith('node:')) { + require(`node:${id}`); + } publicModules.add(id); } } diff --git a/test/parallel/test-process-get-builtin.mjs b/test/parallel/test-process-get-builtin.mjs index 4b840585f2ad33..d983c395488c47 100644 --- a/test/parallel/test-process-get-builtin.mjs +++ b/test/parallel/test-process-get-builtin.mjs @@ -49,7 +49,9 @@ for (const id of publicBuiltins) { const ids = publicBuiltins.add('test'); // Check that import(id).default returns the same thing as process.getBuiltinModule(id). for (const id of ids) { - const prefixed = `node:${id}`; - const imported = await import(prefixed); - assert.strictEqual(process.getBuiltinModule(prefixed), imported.default); + if (!id.startsWith('node:')) { + const prefixed = `node:${id}`; + const imported = await import(prefixed); + assert.strictEqual(process.getBuiltinModule(prefixed), imported.default); + } } diff --git a/test/parallel/test-repl-tab-complete-import.js b/test/parallel/test-repl-tab-complete-import.js index e328d95db5986c..775da78d9613df 100644 --- a/test/parallel/test-repl-tab-complete-import.js +++ b/test/parallel/test-repl-tab-complete-import.js @@ -32,10 +32,12 @@ testMe._domain.on('error', assert.ifError); testMe.complete('import(\'', common.mustCall((error, data) => { assert.strictEqual(error, null); publicModules.forEach((lib) => { - assert( - data[0].includes(lib) && data[0].includes(`node:${lib}`), - `${lib} not found`, - ); + if (!lib.startsWith('node:')) { + assert( + data[0].includes(lib) && data[0].includes(`node:${lib}`), + `${lib} not found`, + ); + } }); const newModule = 'foobar'; assert(!builtinModules.includes(newModule)); @@ -56,8 +58,10 @@ testMe.complete("import\t( 'n", common.mustCall((error, data) => { let lastIndex = -1; publicModules.forEach((lib, index) => { - lastIndex = completions.indexOf(`node:${lib}`); - assert.notStrictEqual(lastIndex, -1); + if (!lib.startsWith('node:')) { + lastIndex = completions.indexOf(`node:${lib}`); + assert.notStrictEqual(lastIndex, -1); + } }); assert.strictEqual(completions[lastIndex + 1], ''); // There is only one Node.js module that starts with n: diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index a38a8e074ab85d..b69192635e6d79 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -61,10 +61,9 @@ require(fixtures.path('resolve-paths', 'default', 'verify-paths.js')); // builtinModules. builtinModules.forEach((mod) => { assert.strictEqual(require.resolve.paths(mod), null); - }); - - builtinModules.forEach((mod) => { - assert.strictEqual(require.resolve.paths(`node:${mod}`), null); + if (!mod.startsWith('node:')) { + assert.strictEqual(require.resolve.paths(`node:${mod}`), null); + } }); // node_modules.