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

Dynamic import error doesn't have error code ERR_MODULE_NOT_FOUND #6699

Open
6 tasks done
hi-ogawa opened this issue Oct 13, 2024 · 1 comment
Open
6 tasks done

Dynamic import error doesn't have error code ERR_MODULE_NOT_FOUND #6699

hi-ogawa opened this issue Oct 13, 2024 · 1 comment
Labels
p2-to-be-discussed Enhancement under consideration (priority)

Comments

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Oct 13, 2024

Describe the bug

For this code:

try {
  await import('no-such-package');
} catch (e) {
  console.log('code:', e.code);
  console.log('message:', e.message);
}

On Node

❯ node src/repro.js
code: ERR_MODULE_NOT_FOUND
message: Cannot find package 'no-such-package' imported from /home/projects/vitest-dev-vitest-53av14/src/repro.js

On Vitest

❯ vitest

stdout | src/repro.test.js > repro
code: undefined
message: Failed to load url no-such-package (resolved id: no-such-package) in /home/projects/vitest-dev-vitest-53av14/src/repro.test.js. Does the file exist?

It looks like this was brought up previously in #326. It doesn't throw during import analysis anymore, but dynamic import error still doesn't have ERR_MODULE_NOT_FOUND code (or maybe it did when it's fixed but regression).

There are some places injecting ERR_MODULE_NOT_FOUND, so maybe it works in some cases, but not the basic case above.

value: 'ERR_MODULE_NOT_FOUND',

Vite SSR injects ERR_MODULE_NOT_FOUND https://github.com/vitest-dev/vite/blob/95020ab49e12d143262859e095025cf02423c1d9/packages/vite/src/node/ssr/fetchModule.ts#L78, so it's working there as well.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-53av14?file=src%2Frepro.test.js

System Info

(stackblitz)

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 2.1.2 
    vite: latest => 5.4.8 
    vitest: latest => 2.1.2

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Vite SSR injects ERR_MODULE_NOT_FOUND vitest-dev/vite@95020ab/packages/vite/src/node/ssr/fetchModule.ts#L78, so it's working there as well.

It only injects modules for externalized dependencies as far as I remember.

I think it might be a good idea to inject the code, but then the message should also be changed to the same one Node.js throws.

@sheremet-va sheremet-va added p2-to-be-discussed Enhancement under consideration (priority) and removed pending triage labels Oct 16, 2024
@sheremet-va sheremet-va moved this to P2 - 2 in Team Board Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-to-be-discussed Enhancement under consideration (priority)
Projects
Status: P2 - 2
Development

No branches or pull requests

2 participants