diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b7fc3f9b2a1ea5..1696c8f43b8c47 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -780,19 +780,19 @@ Module._load = function(request, parent, isMain) { } } - const filename = Module._resolveFilename(request, parent, isMain); - if (StringPrototypeStartsWith(filename, 'node:')) { + 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); @@ -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) + )) { return request; } diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index e7125e39faf7c3..b12220f4d18da5 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -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'/, + }, + ); +}