Repository navigation
require.resolve() and import.meta.resolve() doesn't work with self-referenced packages #47681
Description
Activity
When using require.resolve.paths() from createRequire(), all paths end with node_modules, none of them points to the current project root folder.
- addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Apr 23, 2023 @nodejs/loaders
Reacted by Jesús Leganés-CombarroCan you please provide a simple reproducible example?
In the stack trace you posted, it doesn't look like you are trying to self-reference a package. The error says@mafalda-sfu/mediasoup-cluster-clicannot be found from a file located under@mafalda-sfu/bytenodeifyIn the stack trace you posted, it doesn't look like you are trying to self-reference a package. The error says
@mafalda-sfu/mediasoup-cluster-clicannot be found from a file located under@mafalda-sfu/bytenodeify@mafalda-sfu/bytenodeifyis one of the devDependencies of@mafalda-sfu/mediasoup-cluster-cli, its actual location is/home/piranna/github/Mafalda/Mediasoup-cluster-CLI.I can reproduce this with anything that appears in package.json
exports.Given package.json:
{ "name": "my-package", "exports": { ".": "./main.js" } }Make sure
main.jsexists.In interactive node, run
require.resolve('my-package')from inside the package, and it will correctly return the path to main.jsBut 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
process.cwd()instead of respecting thepathsargument.Edit to add: I reproduced this on both Node v20.5.0 and v16.20.0.
from a different directory not within my-package
The feature is defined as working from within the package. See https://nodejs.org/api/packages.html#self-referencing-a-package-using-its-name:
Within a package, the values defined in the package’s
package.json["exports"](https://nodejs.org/api/packages.html#exports)field can be referenced via the package’s name.So if I set
pathsto a directory within the package it should resolve all the things that work within the package, yes?So if I set
pathsto a directory within the package it should resolve all the things that work within the package, yes?Yes, that's just the definition of self-reference import :-)
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.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jun 3, 2026 This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.Not stale
- addednever-staleIssues and PRs exempt from automated stale handling.Issues and PRs exempt from automated stale handling.and removedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jul 6, 2026
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.