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

module: fix error message for not found #53110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JacksonTian
Copy link
Contributor

@JacksonTian JacksonTian commented May 23, 2024

When I written module path with a typo:

import {readdir} from 'fs/promisesxxx';

The error message tell me cannot find package 'fs'.

$ node test.js 
node:internal/modules/esm/resolve:853
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'fs' imported from /PATH_TO_FILE/test.js
    at packageResolve (node:internal/modules/esm/resolve:853:9)
    at moduleResolve (node:internal/modules/esm/resolve:910:20)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v20.11.1

Actually the fs is a native module, the package should be a full name. This update try to fix it.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels May 23, 2024
@JacksonTian JacksonTian force-pushed the fix_module_not_found_error branch from 521f4a2 to c124e57 Compare May 23, 2024 04:07
@GeoffreyBooth
Copy link
Member

GeoffreyBooth commented May 23, 2024

What bug does this fix?

Please add a description explaining your intent with this PR and why it’s the best solution of alternatives you considered.

@JacksonTian
Copy link
Contributor Author

@GeoffreyBooth Updated.

@aduh95
Copy link
Contributor

aduh95 commented May 23, 2024

Actually the fs is a native module, the package should be a full name. This update try to fix it.

node:fs is, but fs can be a userland module. If you run the following:

mkdir repro && cd repro
mkdir -p node_modules/fs
echo 'export let readdir' > node_modules/fs/promisesxxx.mjs
echo '{"name":"fs","exports":{"./promisesxxx":"./promisesxxx.mjs"}}' > node_modules/fs/package.json
node --input-type=module -e 'import {readdir} from "fs/promisesxxx"'
cd .. && rm -r repro

You can see that the import doesn't raise any error.

{
code: 'ERR_MODULE_NOT_FOUND',
name: 'Error',
message: /Cannot find package 'fs\/promisesx'/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct, the missing package is fs, not fs/promisesx. What you could do is to add the complete specifier – either as Cannot find package 'fs' imported from … while trying to resolve 'fs/promisesx', or as an additional property on the error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO for the end-users, Cannot find package 'fs' will not hint them that fs/promisesx is the not-found package/specifier.

But I'm not an expert on this, so IDK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to the suggestion by @aduh95 as this giving more context.

For builtins, it might even be worth having a different error message, that is builtin specific:

Unable to import builtin module 'fs/promisesx'.

And if we're doing wishful thinking, would be amazing to see levenshtein distance searching:

Unable to import builtin module 'fs/promisesx', did you mean 'fs/promises'?.

Such suggestion error messages could in theory even apply to node_modules packages lookups and exports as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion error messages seem like a good idea to have in general :-)

@JacksonTian
Copy link
Contributor Author

Actually the fs is a native module, the package should be a full name. This update try to fix it.

node:fs is, but fs can be a userland module. If you run the following:

mkdir repro && cd repro
mkdir -p node_modules/fs
echo 'export let readdir' > node_modules/fs/promisesxxx.mjs
echo '{"name":"fs","exports":{"./promisesxxx":"./promisesxxx.mjs"}}' > node_modules/fs/package.json
node --input-type=module -e 'import {readdir} from "fs/promisesxxx"'
cd .. && rm -r repro

You can see that the import doesn't raise any error.

The key point is should report the full package name in error message. It is not related to native module or user-land module.

@aduh95
Copy link
Contributor

aduh95 commented May 24, 2024

The key point is should report the full package name in error message. It is not related to native module or user-land module.

The full package name is 'fs', and that's already what's shown in the error message. If you want to add the full specifier, see my suggestion above.

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. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants