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

[Bug]: Using "module": "NodeNext" causes the error "SyntaxError: Cannot use import statement outside a module" #4464

Closed
chrismeyers opened this issue Jul 22, 2024 · 5 comments
Labels

Comments

@chrismeyers
Copy link

Version

29.2.1+

Steps to reproduce

  1. Clone https://github.com/chrismeyers/reproduce-tsjest-29.2.1-error-nodenext
  2. Run yarn test and see the error
  3. Change the version of ts-jest to the latest (currently 29.2.3), then run yarn test and see the error
  4. Change the version of ts-jest to 29.2.0, then run yarn test and tests should pass

Expected behavior

Tests should pass

Actual behavior

The test suite fails to run

Debug log

ts-jest.log

Additional context

Appears to be very similar to #4439

Environment

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.15.1/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
    bun: 1.1.13 - ~/.bun/bin/bun
  npmPackages:
    jest: 29.7.0 => 29.7.0
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 22, 2024

I think I know the issue. Since we allow the usage of Node16 and NodeNext, the code transpilation takes into account of type: module in package.json which results in compiled codes are in ESM format while your example runs with Jest in CJS mode.

There are 2 ways to workaround your issue:

  • Run tests in ESM mode
  • Or run tests in CJS mode but set module: CommonJS explicitly in tsconfig

Hi @SimenB @mrazauskas can Jest automatically pick up type: "module" in package.json to detect that it needs to run in ESM mode?
From ts-jest side, we doesn't have the information of package.json so it's very hard for us to warn about "user should not use Node16/NodeNext with type: "module" in CJS mode"

@chrismeyers
Copy link
Author

I can confirm that changing to "module": "ESNext" or "module": "CommonJS" fixes the error. I'm guessing NodeNext stopped working after #4429?

For a bit more context, I'm using tshy which is fairly opinionated and generates code using NodeNext. Ideally this can go back to working like it did prior to 29.2.1 so that the tests and built code use the same module and moduleResolution settings, but at least there's a path forward.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 23, 2024

Actually before the fix, Node16 and NodeNext were ignored during code transpilation. The default module was always CommonJS when transpiling codes for Jest when it ran in CJS mode.

After the fix, we take into account of Node16 and NodeNext when transpiling codes. This results in the failed tests you saw. As TypeScript documentation states at https://www.typescriptlang.org/tsconfig/#node16nodenext

If we revert back, the support for these 2 module values won’t be possible. Ideally, Jest should be able to run in ESM mode when it sees type: module.

On a side note, I saw your example repo uses ESM preset but the script to run test is without --experimental-vm-modules which made Jest not able to run ESM codes.

I don’t think it’s possible for us to fix this issue. We highly depend on what Jest wants to accept.

@chrismeyers
Copy link
Author

I see, this is all starting to make more sense. I didn't realize that the --experimental-vm-modules flag has to be provided to enable ESM mode in Jest and that ts-jest emitted CJS otherwise before these changes.

Updating the test script in my example to node --experimental-vm-modules ./node_modules/.bin/jest fixed the tests with no other changes to the tsconfig.json or jest.config.json.

I appreciate your time looking into this! I think it's okay to close this issue.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 23, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants