Skip to content

Commit

Permalink
module: add prefix-only modules to module.builtinModules
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 8, 2024
1 parent 2960a59 commit 0d8ea8b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

<!--type=misc-->
Expand Down
11 changes: 7 additions & 4 deletions lib/internal/bootstrap/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,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();

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-internal-module-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ if (process.argv[2] === 'child') {
});
} else {
require(id);
if (!id.startsWith('node:')) {
require(`node:${id}`);
}
publicModules.add(id);
}
}
Expand Down

0 comments on commit 0d8ea8b

Please sign in to comment.