Skip to content

Commit 0d8ea8b

Browse files
committed
module: add prefix-only modules to module.builtinModules
Fixes #42785
1 parent 2960a59 commit 0d8ea8b

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

doc/api/module.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ Module.isBuiltin = BuiltinModule.isBuiltin;
420420
*/
421421
function initializeCJS() {
422422
// This need to be done at runtime in case --expose-internals is set.
423-
const builtinModules = BuiltinModule.getCanBeRequiredByUsersWithoutSchemeList();
424-
Module.builtinModules = ObjectFreeze(builtinModules);
423+
424+
Module.builtinModules = ObjectFreeze(BuiltinModule.getAllBuiltinModuleIds());
425425

426426
initializeCjsConditions();
427427

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

Lines changed: 3 additions & 0 deletions
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)