Skip to content

VS Code official jest extension gets TS5110 module must be set to NodeNext #31358

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
1 of 4 tasks
observstream opened this issue May 27, 2025 · 1 comment
Open
1 of 4 tasks

Comments

@observstream
Copy link

observstream commented May 27, 2025

Current Behavior

I have been dealing with an issue where I get TSError: ⨯ Unable to compile TypeScript: error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'. when I try to run the tests inside of VS Code using either the run or debug behavior. The tests are working just fine in nx versions 20 or 19 and seems to be related to using the new nodenext module and moduleResolution behavior in version 21.

I dug into this in depth and the issue comes from the jest-config library being used to read the typescript files. The library is being used to read the base jest.config.ts file and also uses the root tsconfig.json file which has the compiler options below:

{
  "compilerOptions": {
    "composite": true,
    "declarationMap": true,
    "emitDeclarationOnly": true,
    "importHelpers": true,
    "isolatedModules": true,
    "lib": ["es2022"],
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es2022",
    "customConditions": ["development"]
  }
}

The problem stems from this line in the jest-config npm package (see below). Because this sets the module to CommonJS, when NX sets the moduleResolution to NodeNext it will always cause the tests to fail. There isn't an easy to override this and it is being updated in jest version 30.

async function registerTsNode(): Promise<Service> {
  try {
    // Register TypeScript compiler instance
    const tsNode = await import('ts-node');
    return tsNode.register({
      compilerOptions: {
        module: 'CommonJS',
      },
      moduleTypes: {
        '**': 'cjs',
      },
    });
  } catch (e: any) {
    if (e.code === 'ERR_MODULE_NOT_FOUND') {
      throw new Error(
        `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}`,
      );
    }

    throw e;
  }
}

Expected Behavior

Jest tests should be able to be run using vs code standard extensions. I was able to fix this in my project by creating a javascript jest.config.js file at the root of my project and adding the following override

import { getJestProjectsAsync } from '@nx/jest';

process.env.TS_NODE_COMPILER_OPTIONS = JSON.stringify({
  moduleResolution: 'Node10',
  customConditions: null,
});

export default async () => ({
  projects: await getJestProjectsAsync(),
});

GitHub Repo

https://github.com/observstream/testing-nx-test

Steps to Reproduce

  1. Create new node project using npx create-nx-workspace and use jest as the test runner
  2. Add file into the src directory and add a spec file
  3. Open project with either https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest or https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner
  4. Open test file and try to run test by clicking on one of the test runners
  5. See error in console

Nx Report

Node           : 22.14.0
OS             : darwin-arm64
Native Target  : aarch64-macos
npm            : 10.9.2

nx (global)        : 21.0.3
nx                 : 21.1.2
@nx/js             : 21.1.2
@nx/jest           : 21.1.2
@nx/eslint         : 21.1.2
@nx/workspace      : 21.1.2
@nx/devkit         : 21.1.2
@nx/esbuild        : 21.1.2
@nx/eslint-plugin  : 21.1.2
@nx/node           : 21.1.2
typescript         : 5.7.3
---------------------------------------
Registered Plugins:
@nx/js/typescript
@nx/eslint/plugin
@nx/jest/plugin
---------------------------------------
Cache Usage: 1.51 KB / 92.64 GB

Failure Logs

Error: Jest: Failed to parse the TypeScript config file .../testing-nx-tests/apps/testing-nx-tests-e2e/jest.config.ts
  TSError: ⨯ Unable to compile TypeScript:
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.

    at readConfigFileAndSetRootDir (.../testing-nx-tests/node_modules/@jest/core/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:116:13)
    at async readInitialOptions (.../testing-nx-tests/node_modules/@jest/core/node_modules/jest-config/build/index.js:403:13)
    at async readConfig (.../testing-nx-tests/node_modules/@jest/core/node_modules/jest-config/build/index.js:147:48)
    at async Promise.all (index 0)
    at async readConfigs (.../testing-nx-tests/node_modules/@jest/core/node_modules/jest-config/build/index.js:443:27)
    at async runCLI (.../testing-nx-tests/node_modules/@jest/core/build/cli/index.js:151:59)
    at async Object.run (.../testing-nx-tests/node_modules/jest-cli/build/run.js:130:37)

Package Manager Version

yarn or npm

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@Wykks
Copy link

Wykks commented May 27, 2025

Duplicate of #31097 although your workaround seems better 👍

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

No branches or pull requests

2 participants