Skip to content

module: fix require() of package imports mapped to builtins - #68006

Open
neoLsH wants to merge 1 commit into
nodejs:mainfrom
neoLsH:fix-cjs-package-imports-builtin
Open

neoLsH wants to merge 1 commit into
nodejs:mainfrom
neoLsH:fix-cjs-package-imports-builtin

Conversation

@neoLsH

@neoLsH neoLsH commented Dec 4, 2026

Copy link
Copy Markdown

Fixes #49257.

require() of a package import that resolves to a builtin module (for example "#path": "path") currently throws:

TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file

packageImportsResolve() returns a node:path URL. finalizeEsmResolution() then passes it to fileURLToPath(), which only accepts file: URLs. The ESM resolver already returns non-file URLs from packageImportsResolve() without calling finalizeResolution().

This change makes the CJS loader return the normalized builtin id when the resolved URL uses the node: protocol, so the existing builtin loading path handles it.

Test: added #builtin to the pkgimports fixture and covered both require('#builtin') and import('#builtin') in test/es-module/test-esm-imports.mjs.

Verification (local build of v26.14.0 source with this patch; the patched function is unchanged on current main):

  • Before: node assert.cjs -> ERR_INVALID_URL_SCHEME; node assert.mjs -> OK.
  • After: out/Release/node test/es-module/test-esm-imports.mjs -> pass.
  • After: python3 tools/test.py test/es-module/test-esm-imports.mjs -> +1 -0.
  • After: both reproduction scripts -> OK.
  • git diff --check -> clean.

AI disclosure: I used an AI coding assistant to help investigate and draft this change. I reviewed the diff and personally verified the failing and passing test results above.

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: nodejs#49257
Signed-off-by: neoLsH <906344049@qq.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Referencing external dependency via package.json works for ESM, but not CJS

2 participants