From bdccb4117e81c303fbf4bbb123f66250382ad083 Mon Sep 17 00:00:00 2001 From: neoLsH <906344049@qq.com> Date: Fri, 4 Dec 2026 10:57:54 +0800 Subject: [PATCH] module: fix require() of package imports mapped to builtins Package imports such as `"#path": "path"` resolve to a `node:` URL. The CJS loader passed that URL to `fileURLToPath()`, which only accepts `file:` URLs, so `require('#path')` threw `ERR_INVALID_URL_SCHEME` while `import('#path')` worked. Return the normalized builtin id when the resolved URL uses the `node:` protocol. This mirrors the ESM resolver, which returns non-file URLs without finalizing them as file paths. Add a regression test covering both `require()` and `import()` of a package import that targets a builtin. Fixes: https://github.com/nodejs/node/issues/49257 Signed-off-by: neoLsH <906344049@qq.com> --- lib/internal/modules/cjs/loader.js | 5 +++++ test/es-module/test-esm-imports.mjs | 10 ++++++++++ test/fixtures/es-modules/pkgimports/package.json | 1 + 3 files changed, 16 insertions(+) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 0ce49516f9c9..30c05e1e081f 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1652,6 +1652,11 @@ function finalizeEsmResolution(resolved, parentPath, pkgPath) { throw new ERR_INVALID_MODULE_SPECIFIER( resolved, 'must not include encoded "/" or "\\" characters', parentPath); } + // A package import may resolve to a builtin module, which is represented + // as a node: URL and has no file path to resolve. + if (resolved.protocol === 'node:') { + return BuiltinModule.normalizeRequirableId(resolved.href); + } const filename = fileURLToPath(resolved); const actual = tryFile(filename); if (actual) { diff --git a/test/es-module/test-esm-imports.mjs b/test/es-module/test-esm-imports.mjs index 14c62ce6d774..52ba125e41f4 100644 --- a/test/es-module/test-esm-imports.mjs +++ b/test/es-module/test-esm-imports.mjs @@ -1,5 +1,6 @@ import { mustCall } from '../common/index.mjs'; import assert from 'assert'; +import path from 'node:path'; import importer from '../fixtures/es-modules/pkgimports/importer.js'; import { requireFixture } from '../fixtures/pkgexports.mjs'; @@ -122,6 +123,15 @@ const { requireImport, importImport } = importer; } }); +// Package imports can resolve to a builtin module. The CJS loader must load +// the resolved node: URL as a builtin instead of converting it to a file path. +requireImport('#builtin').then(mustCall(({ default: actual }) => { + assert.strictEqual(actual, path); +})); +importImport('#builtin').then(mustCall((actual) => { + assert.strictEqual(actual.default, path); +})); + // CJS resolver must still support #package packages in node_modules requireFixture('#cjs').then(mustCall((actual) => { assert.strictEqual(actual.default, 'cjs backcompat'); diff --git a/test/fixtures/es-modules/pkgimports/package.json b/test/fixtures/es-modules/pkgimports/package.json index 82e57b11ce45..795a39f6abce 100644 --- a/test/fixtures/es-modules/pkgimports/package.json +++ b/test/fixtures/es-modules/pkgimports/package.json @@ -10,6 +10,7 @@ "#subpath/null": null, "#subpath/*.asdf": "./test.js", "#external": "pkgexports/valid-cjs", + "#builtin": "path", "#external/subpath/*": "pkgexports/sub/*", "#external/invalidsubpath/": "pkgexports/sub", "#belowbase": "../belowbase",