Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,19 @@ Module._load = function(request, parent, isMain) {
}
}

const filename = Module._resolveFilename(request, parent, isMain);
if (StringPrototypeStartsWith(filename, 'node:')) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading a builtin module prefixed with "node:" expects "ERR_UNKNOWN_BUILTIN_MODULE" to be thrown.
If the "resolve" function is called here, "MODULE_NOT_FOUND" will be thrown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERR_UNKNOWN_BUILTIN_MODULE certainly makes more sense here than MODULE_NOT_FOUND, I agree (although I think both are fine).

if (StringPrototypeStartsWith(request, 'node:')) {
// Slice 'node:' prefix
const id = StringPrototypeSlice(filename, 5);
const id = StringPrototypeSlice(request, 5);

const module = loadNativeModule(id, request);
if (!module?.canBeRequiredByUsers) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(filename);
throw new ERR_UNKNOWN_BUILTIN_MODULE(request);
}

return module.exports;
}

const filename = Module._resolveFilename(request, parent, isMain);
const cachedModule = Module._cache[filename];
if (cachedModule !== undefined) {
updateChildren(parent, cachedModule, true);
Expand Down Expand Up @@ -854,9 +854,14 @@ Module._load = function(request, parent, isMain) {
};

Module._resolveFilename = function(request, parent, isMain, options) {
if (StringPrototypeStartsWith(request, 'node:') ||
(NativeModule.canBeRequiredByUsers(request) &&
NativeModule.canBeRequiredWithoutScheme(request))) {
if ((
StringPrototypeStartsWith(request, 'node:') &&
NativeModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
) ||
(
NativeModule.canBeRequiredByUsers(request) &&
NativeModule.canBeRequiredWithoutScheme(request)
)) {
Comment thread
zhmushan marked this conversation as resolved.
Outdated
return request;
}

Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-require-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));
assert.strictEqual(resolvedPaths.includes('/node_modules'), false);
});
}

{
assert.strictEqual(require.resolve('node:test'), 'node:test');
assert.strictEqual(require.resolve('node:fs'), 'node:fs');

assert.throws(
() => require.resolve('node:unknown'),
{
code: 'MODULE_NOT_FOUND',
message: /^Cannot find module 'node:unknown'/,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quote from the documentation:

If the module can not be found, a MODULE_NOT_FOUND error is thrown.

},
Comment thread
zhmushan marked this conversation as resolved.
Outdated
);
}