Skip to content

module: fix error message for not found #53110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ function packageResolve(specifier, base, conditions) {

// eslint can't handle the above code.
// eslint-disable-next-line no-unreachable
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
throw new ERR_MODULE_NOT_FOUND(specifier, fileURLToPath(base), null);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions test/es-module/test-esm-loader-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ assert.throws(
message: /Cannot find package 'target'/
}
);

assert.throws(
() => resolve('fs/promisesx'),
{
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 :-)

}
);