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

misleading error: Cannot find package '.../package.json', when package.json exists #49674

Open
isaacs opened this issue Sep 16, 2023 · 3 comments · May be fixed by #49728
Open

misleading error: Cannot find package '.../package.json', when package.json exists #49674

isaacs opened this issue Sep 16, 2023 · 3 comments · May be fixed by #49728

Comments

@isaacs
Copy link
Contributor

isaacs commented Sep 16, 2023

Version

>=20.5.0

Platform

Darwin moxy.lan 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64

Subsystem

modules

What steps will reproduce the bug?

mkdir -p node_modules/whatever
echo {} > node_modules/whatever/package.json
node -e 'import("whatever")'

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

Happens when a package.json exists, in a package folder that exists, but which does not define a main or '.' export.

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

The previous behavior was:

Error: Cannot find package '/Users/isaacs/dev/isaacs/resolve-import/test/fixtures/node_modules/whatever/' imported from /Users/isaacs/dev/isaacs/resolve-import/test/fixtures/[eval]

However, this is also not great, because it implies that the folder could not be found, when it clearly can be found.

What do you see instead?

Error: Cannot find package '/Users/isaacs/dev/isaacs/resolve-import/test/fixtures/node_modules/whatever/package.json' imported from /Users/isaacs/dev/isaacs/resolve-import/test/fixtures/[eval]

Additional information

The confusing bit is that it's saying it can't "find" the package.json file, which clearly does exist and is a valid package.json (albeit not one that defines a valid importable package).

A better error message would be something like:

Error: Package is not importable '/Users/isaacs/dev/isaacs/resolve-import/test/fixtures/node_modules/whatever/' imported from /Users/isaacs/dev/isaacs/resolve-import/test/fixtures/[eval]
@isaacs
Copy link
Contributor Author

isaacs commented Sep 16, 2023

Also interesting note: it doesn't raise an error if there's a index.js in the folder, because in that case, it has a load target.

So the problem is really not the package.json not being findable, or the folder not being findable, but the package at node_modules/whatever not being something that Node can load successfully.

@himself65
Copy link
Member

in master build

> node/out/Debug/node -e 'import("whatever")'
node:internal/modules/esm/resolve:186
  const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
                         ^

Error: Cannot find package '/Users/himself65/Code/node-issue-49674/node_modules/whatever/package.json' imported from /Users/himself65/Code/node-issue-49674/[eval]
    at legacyMainResolve (node:internal/modules/esm/resolve:186:26)
    at packageResolve (node:internal/modules/esm/resolve:771:14)
    at moduleResolve (node:internal/modules/esm/resolve:833:20)
    at defaultResolve (node:internal/modules/esm/resolve:1038:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:376:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:345:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:221:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:308:34)
    at importModuleDynamically (node:internal/process/execution:89:31)
    at importModuleDynamicallyWrapper (node:internal/vm/module:431:21) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v21.0.0-pre

@himself65
Copy link
Member

Please see #49728

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants