Skip to content
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

ESM doesn't load module in NODE_PATH #38687

Closed
8HoLoN opened this issue May 14, 2021 · 6 comments
Closed

ESM doesn't load module in NODE_PATH #38687

8HoLoN opened this issue May 14, 2021 · 6 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@8HoLoN
Copy link

8HoLoN commented May 14, 2021

  • Version: v16.1.0
  • Platform: Microsoft Windows NT 10.0.19042.0 x64
  • Subsystem:

What steps will reproduce the bug?

set NODE_PATH=C:\foo
node test.js

  • C:\foo\bar\baz.js
  • C:\foo\bar\package.json
  • C:\somedir\test.js
  • C:\somedir\package.json

Files

  • test.js
import { b } from "bar/baz.js";
console.log(b);
  • baz.js
const b = 2;
export { b }
  • package.json
{   
    "type":"module"
}

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

Everytime

What is the expected behavior?

Load the module as if it was not in other directory.
it says Did you mean to import bar/baz.js?. Yes it's what I want in result of import { b } from "bar/baz.js";.

What do you see instead?

C:\somedir>node test.js
node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'bar' imported from C:\somedir\test.js
Did you mean to import bar/baz.js?
←[90m    at new NodeError (node:internal/errors:363:5)←[39m
←[90m    at packageResolve (node:internal/modules/esm/resolve:698:9)←[39m
←[90m    at moduleResolve (node:internal/modules/esm/resolve:739:18)←[39m
←[90m    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)←[39m
←[90m    at Loader.resolve (node:internal/modules/esm/loader:89:40)←[39m
←[90m    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)←[39m
←[90m    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)←[39m
←[90m    at link (node:internal/modules/esm/module_job:72:36)←[39m {
  code: ←[32m'ERR_MODULE_NOT_FOUND'←[39m
}

Additional information

@8HoLoN 8HoLoN changed the title ESM don't load module in NODE_PATH ESM doesn't load module in NODE_PATH May 14, 2021
@aduh95
Copy link
Contributor

aduh95 commented May 14, 2021

This was done intentionally IIRC, and is documented here: https://nodejs.org/api/esm.html#esm_no_node_path

Closing as there's no bug here, but feel free to continue the discussion or reopen if you think the documentation can be improved.

@aduh95 aduh95 closed this as completed May 14, 2021
@aduh95 aduh95 added the esm Issues and PRs related to the ECMAScript Modules implementation. label May 14, 2021
@aduh95
Copy link
Contributor

aduh95 commented May 14, 2021

Related discussion: nodejs/modules#534

@8HoLoN
Copy link
Author

8HoLoN commented May 14, 2021

No NODE_PATH# NODE_PATH is not part of resolving import specifiers. Please use symlinks if this behavior is desired.
So, that means that we have to create a symlink in every folder where a js file would import a file from that global folder.
It's heavier, isn't it ? What is the reason for not having this feature ?

@aduh95
Copy link
Contributor

aduh95 commented May 14, 2021

nodejs/modules#534 (comment) gives some suggestions on how to workaround this limitation. If symlinks are a pain to put in place, you could have a node_modules folder at the root of your file system instead, or you could use a custom loader (although the loader API is not yet stabilised).

What is the reason for not having this feature ?

I think it's because it was simplifying the PACKAGE_RESOLVE algorithm, and no one complained for the lack of this feature during the development of the ESM implementation.

@8HoLoN
Copy link
Author

8HoLoN commented May 14, 2021

OK thx for information.

@aral
Copy link

aral commented Jan 22, 2022

If you’re using experiemental ES Module loaders, one workaround is to carry out module resolution yourself for those modules that you want to include from a different path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

3 participants