node icon indicating copy to clipboard operation
node copied to clipboard

module: fix error message for not found

Open JacksonTian opened this issue 1 year ago β€’ 5 comments

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.

JacksonTian avatar May 23 '24 04:05 JacksonTian

Review requested:

  • [ ] @nodejs/loaders

nodejs-github-bot avatar May 23 '24 04:05 nodejs-github-bot

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 avatar May 23 '24 05:05 GeoffreyBooth

@GeoffreyBooth Updated.

JacksonTian avatar May 23 '24 05:05 JacksonTian

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.

aduh95 avatar May 23 '24 07:05 aduh95

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.

JacksonTian avatar May 24 '24 06:05 JacksonTian

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.

aduh95 avatar May 24 '24 09:05 aduh95