Skip to content

Commit 0dee8be

Browse files
committed
module: add prefix-only modules to module.builtinModules
Fixes nodejs#42785
1 parent 7443a28 commit 0dee8be

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

doc/api/module.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ added:
2828
A list of the names of all modules provided by Node.js. Can be used to verify
2929
if a module is maintained by a third party or not.
3030

31-
Note: the list doesn't contain [prefix-only modules][] like `node:test`.
31+
Note: the list also contains [prefix-only modules][] like `node:test`.
3232

3333
`module` in this context isn't the same object that's provided
3434
by the [module wrapper][]. To access it, require the `Module` module:

doc/api/modules.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ Some built-in modules are always preferentially loaded if their identifier is
513513
passed to `require()`. For instance, `require('http')` will always
514514
return the built-in HTTP module, even if there is a file by that name. The list
515515
of built-in modules that can be loaded without using the `node:` prefix is exposed
516-
as [`module.builtinModules`][].
516+
in [`module.builtinModules`][], listed without the prefix.
517517

518518
### Built-in modules with mandatory `node:` prefix
519519

@@ -527,6 +527,8 @@ taken the name. Currently the built-in modules that requires the `node:` prefix
527527
* [`node:test`][]
528528
* [`node:test/reporters`][]
529529

530+
The list of these modules is exposed in [`module.builtinModules`][], including the prefix.
531+
530532
## Cycles
531533

532534
<!--type=misc-->

lib/internal/bootstrap/realm.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,17 @@ class BuiltinModule {
320320
);
321321
}
322322

323-
static getCanBeRequiredByUsersWithoutSchemeList() {
324-
return ArrayFrom(canBeRequiredByUsersWithoutSchemeList);
325-
}
326-
327323
static getSchemeOnlyModuleNames() {
328324
return ArrayFrom(schemelessBlockList);
329325
}
330326

327+
static getAllBuiltinModuleIds() {
328+
return [
329+
...canBeRequiredByUsersWithoutSchemeList,
330+
...ArrayFrom(schemelessBlockList, (x) => `node:${x}`),
331+
];
332+
}
333+
331334
// Used by user-land module loaders to compile and load builtins.
332335
compileForPublicLoader() {
333336
if (!BuiltinModule.canBeRequiredByUsers(this.id)) {

lib/internal/modules/cjs/loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ Module.isBuiltin = BuiltinModule.isBuiltin;
434434
*/
435435
function initializeCJS() {
436436
// This need to be done at runtime in case --expose-internals is set.
437-
const builtinModules = BuiltinModule.getCanBeRequiredByUsersWithoutSchemeList();
438-
Module.builtinModules = ObjectFreeze(builtinModules);
437+
438+
Module.builtinModules = ObjectFreeze(BuiltinModule.getAllBuiltinModuleIds());
439439

440440
initializeCjsConditions();
441441

test/parallel/test-internal-module-require.js

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ if (process.argv[2] === 'child') {
8787
});
8888
} else {
8989
require(id);
90+
if (!id.startsWith('node:')) {
91+
require(`node:${id}`);
92+
}
9093
publicModules.add(id);
9194
}
9295
}

0 commit comments

Comments
 (0)