module: fix error message for not found
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.
Review requested:
- [ ] @nodejs/loaders
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.
@GeoffreyBooth Updated.
Actually the
fsis 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.
Actually the
fsis a native module, the package should be a full name. This update try to fix it.
node:fsis, butfscan 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 reproYou 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.
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.