Sphere
Log in Join

Jest 'Cannot use import statement outside a module' with ts-jest and ESM

I've been doing Jest configs for years and this one still took me a morning. We moved a small TypeScript package to "type": "module" so Node 20 could import it directly, and every single test started failing on the first line:

FAIL  src/format.test.ts
● Test suite failed to run

SyntaxError: Cannot use import statement outside a module

at Runtime._execModule (node_modules/jest-runtime/build/index.js:1446:24)
at Object.<anonymous> (src/format.ts:1:1)

Jest 29.7, ts-jest 29.1.2, TypeScript 5.4, Node 20.11. The tsconfig had "module": "ESNext", so tsc was happy. Jest wasn't.

## Why Jest didn't get the memo

Jest doesn't read your tsconfig to decide how to load modules — it uses its own runtime, which defaults to CommonJS. ts-jest was transforming the TypeScript but emitting ESM syntax, which Jest then tried to evaluate as CJS. Hence the error on import.

The usual ts-jest setup silently sets module: CommonJS for you, which is why it works until you add "type": "module". That field tells Node and jest-resolve that .js files in the package are ESM, and things fall apart.

## The ESM preset

Use the dedicated preset instead of the default one:

js
// jest.config.cjs
module.exports = {
preset: "ts-jest/presets/default-esm",
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
transform: {
"^.+\\.tsx?$": ["ts-jest", { useESM: true }],
},
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
};

Note the config file is .cjs. With "type": "module" in package.json, a jest.config.js is treated as ESM and Jest won't load it as CommonJS unless you use export default. Renaming to .cjs avoids the question entirely.

extensionsToTreatAsEsm is the bit I missed first. Without it, Jest still treats .ts as CJS even with the ESM preset, and you get the same SyntaxError.

## Why the extension mapper is needed

TypeScript ESM code writes relative imports with a .js extension even though the source file is .ts:

ts
import { formatDate } from "./format.js";

At runtime that resolves fine because tsc emits format.js. Under Jest with ts-jest, the file on disk is format.ts, so resolution fails with Cannot find module './format.js'. The moduleNameMapper above strips the extension. It looks odd but it's the documented approach.

## The NODE_OPTIONS part

Even with all that, Jest's ESM support in 29.x still needs a flag on Node 20:

bash
NODE_OPTIONS=--experimental-vm-modules npx jest

Skip it and you get SyntaxError: Unexpected token 'export' from a different place, which is how you know you forgot. I put it in the test script:

json
{
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
}
}

That syntax is POSIX-only. On Windows we use cross-env, or just run the tests in the Linux container like everyone else on the team.

## Is it worth it

If your package doesn't need to be imported as ESM by Node directly, the easy path is to keep "type": "commonjs" and let ts-jest do its default thing. We genuinely needed ESM for a CLI that dynamic-imports plugins, so we ate the config. But I'd think twice before doing this just because ESM is newer.

Anyway, four config changes and one env var. Not fun, but at least the errors are specific once you know which one you're looking at.