Skip to content

global module/package resolution problem for ESM #49670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cyberphone opened this issue Sep 16, 2023 · 4 comments
Closed

global module/package resolution problem for ESM #49670

cyberphone opened this issue Sep 16, 2023 · 4 comments
Labels
wrong repo Issues that should be opened in another repository.

Comments

@cyberphone
Copy link

Version

v18.17.1

Platform

Linux ubuntu 5.15.0-83-generic #92-Ubuntu SMP Mon Aug 14 09:30:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

npm install cbor-object -g

"added 2 packages in 3s"

npm list -g

├── [email protected]
├── [email protected]
└── [email protected]

node test.js

node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
throw err;
^

Error: Cannot find module 'cbor-object'
Require stack:

  • /home/demo/cbor-test/test.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object. (/home/demo/cbor-test/test.js:2:14)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/home/demo/cbor-test/test.js' ]
    }

comment: this is rather strange given that npm found the module. Continued...

export NODE_PATH=/home/demo/node-v18.17.1-linux-x64/lib/node_modules
node test.js

Now it worked! Continued...

node test.mjs

node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'cbor-object' imported from /home/demo/cbor-test/test.mjs
Did you mean to import cbor-object/cjs/index.cjs?
at new NodeError (node:internal/errors:405:5)
at packageResolve (node:internal/modules/esm/resolve:887:9)
at moduleResolve (node:internal/modules/esm/resolve:936:20)
at defaultResolve (node:internal/modules/esm/resolve:1129:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap. (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

comment: Node has obviously found the module since it refers to cbor-object/cjs/index.cjs which is a part of the associated package.json file:

{
  "name": "cbor-object",
  "version": "1.0.1",
  "description": "CBOR: deterministic encoder/decoder, bidirectional diagnostic notation, and utilities",
  "homepage": "https://github.com/cyberphone/CBOR.js#cborjs",
  "repository": {
    "type": "git",
    "url": "https://github.com/cyberphone/CBOR.js"
  },
  "keywords": [ "cbor", "RFC8949", "deterministic" ],
  "exports": {
    ".": {
      "import": "./mjs/index.mjs",
      "require": "./cjs/index.cjs"
    }
  },
  "scripts": {
    "test": "node ./test-all.mjs"
  },
  "author": {
    "name": "Anders Rundgren",
    "email": "[email protected]"
  },
  "engines": {
    "node": ">=14.18.1"
  },
  "license": "MIT",
  "dependencies": {
    "npm": "*"
  }
}

comment: although npm link cbor-object works, it defeats the value of global installations.

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

Always

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

Global should (IMO) not require any specific actions

What do you see instead?

Global package/module resolution working as for npm

Additional information

Gives identical results on W11

@aduh95
Copy link
Contributor

aduh95 commented Sep 16, 2023

That seems to be an issue with a different project (NPM), not Node.js. FYI NPM development is happening at https://github.com/npm/cli.

@aduh95 aduh95 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2023
@aduh95 aduh95 added the wrong repo Issues that should be opened in another repository. label Sep 16, 2023
@cyberphone
Copy link
Author

Thanx for the quick response @aduh95!
Since the workaround (setting NODE_PATH) works flawlessly for CJS, there seems to be a bug (or "by design" limitation), in the ESM resolver.

Although NPM is not the same project as Node.js, it is hard to see the point having different conventions. If NPM can find global modules, why can't Node.js?

@aduh95
Copy link
Contributor

aduh95 commented Sep 16, 2023

@cyberphone apologies, I read past that. This limitation is documented in the docs: https://nodejs.org/api/esm.html#esm_no_node_path
I’ll try to find the issue where this was discussed. EDIT: here it is nodejs/modules#534

@cyberphone
Copy link
Author

Thanx @aduh95 !
I don't know/understand the rationale for outlawing NODE_PATH for ESM but I guess this won't change 😪

It was the error message put me on the wrong track since it shows that the associated package.json indeed was found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

2 participants