-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
require.resolve()
and import.meta.resolve()
doesn't work with self-referenced packages
#47681
Comments
When using require.resolve.paths() from createRequire(), all paths end with node_modules, none of them points to the current project root folder. |
@nodejs/loaders |
Can you please provide a simple reproducible example? |
|
I can reproduce this with anything that appears in package.json Given package.json: {
"name": "my-package",
"exports": {
".": "./main.js"
}
} Make sure In interactive node, run But then from a different directory not within my-package, again enter node interactive cli and this time try: require.resolve('my-package', { paths: [ pathToMyPackage ] }) It will throw MODULE_NOT_FOUND. This seems to show that the self-reference lookup is always using Edit to add: I reproduced this on both Node v20.5.0 and v16.20.0. |
The feature is defined as working from within the package. See https://nodejs.org/api/packages.html#self-referencing-a-package-using-its-name:
|
So if I set |
Yes, that's just the definition of self-reference import :-) |
Version
v20.0.0
Platform
Linux executive 5.19.0-40-generic #41-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 23 21:39:15 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
require, import
What steps will reproduce the bug?
Call to
require.resolve()
created withcreateRequire()
, or toimport.meta.resolve()
, passing the package name to resolve to itself.How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior? Why is that the expected behavior?
It should return the path of the package itself.
What do you see instead?
Additional information
Not sure if it happens with CommonJS
require()
too, or just only with ESM related resolve functions.The text was updated successfully, but these errors were encountered: