Skip to content

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

Open

Description

@remcohaszing

Version

v20.5.1

Platform

Linux vali 6.4.6-76060406-generic #202307241739169092810522.04~d567a38 SMP PREEMPT_DYNAMIC Tue A x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Clone https://gist.github.com/remcohaszing/25c719651d340d9170dd75428dbcef8e

Given the following files:

// package.json
{
  "imports": {
    "#path": {
      "default": "path"
    }
  }
}
// index.cjs
require('#path')
// index.mjs
await import('#path')

Now run both scripts.

How often does it reproduce? Is there a required condition?

It reproduces consistently.

What is the expected behavior? Why is that the expected behavior?

Both scripts run and output nothing.

What do you see instead?

Running node index.mjs works fine, but node index.cjs throws:

node:internal/modules/cjs/loader:1018
        throw e;
        ^

TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at new NodeError (node:internal/errors:405:5)
    at fileURLToPath (node:internal/url:1397:11)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1059:20)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1011:16)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/home/remco/Downloads/foo/index.cjs:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10) {
  code: 'ERR_INVALID_URL_SCHEME'
}

Node.js v20.5.1

Referencing a relative path works fine though.

Additional information

There was some discussion in this Twitter thread.

Activity

  1. bmeck commented on Aug 20, 2023

    @bmeck
    Member

    The problem is finalizeESMResolution in the CJS loader isn't checking if it is a builtin before performing URL to file path conversion:

    const filename = fileURLToPath(resolved);

    Likely the solution would be to add some tests for node: prefixes and without using BuiltinModule.isBuiltin

  2. Andarist commented on Aug 20, 2023

    @Andarist
    Contributor

    U suggest specific solution for a builtin module. Does it mean that it shouldnt be allowed to do the same with a regular external pkg?

  3. bmeck commented on Aug 20, 2023

    @bmeck
    Member

    @Andarist CJS cannot load data: or http/s: so that should be sufficient. These are already resolved to an absolute URL by that point.

  4. github-actions commented on May 28, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  5. added
    stale
    on May 28, 2026
  6. remcohaszing commented on May 29, 2026

    @remcohaszing
    ContributorAuthor

    This issue is still present.

  7. removed
    stale
    on May 30, 2026
  8. github-actions commented on Aug 28, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  9. added
    stale
    on Aug 28, 2026
  10. segevfiner commented on Aug 28, 2026

    @segevfiner
    Contributor

    Bump

  11. removed
    stale
    on Aug 29, 2026
  12. github-actions commented on Nov 28, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  13. added
    stale
    on Nov 28, 2026
  14. segevfiner commented on Nov 28, 2026

    @segevfiner
    Contributor

    Bump

  15. removed
    stale
    on Nov 29, 2026
  16. added
    confirmed-bug
    never-stale
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugnever-stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions