diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b7fc3f9b2a1ea5..61f91eca9f6ab5 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -854,9 +854,11 @@ 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))) { + return request; + } else if (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..657b704afaf620 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -80,3 +80,18 @@ require(fixtures.path('resolve-paths', 'default', 'verify-paths.js')); assert.strictEqual(resolvedPaths.includes('/node_modules'), false); }); } + +// For direct use of require expressions inside of CJS modules, +// all kinds of specifiers should work without issue. +{ + 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'/, + }, + ); +}