Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest throws SyntaxError on import in a test or dependency

ErrorSyntaxError: Cannot use import statement outside a module

Short answer

Jest runs files as CommonJS unless a transform changes them. The error means an import line reached Node untransformed. Either your own code has no Babel config, a dependency in node_modules ships ES modules that Jest skips by default, or the project is ESM and Jest is not started in ESM mode.

  • Applies to Jest 30, Babel 7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the path in the error. Inside src means your own code is not transformed; inside node_modules means a package ships ESM.

    npx jest 2>&1 | grep -B2 -A6 "Cannot use import"
  2. 2Match what you see, then jump to its fix

    1. 1The path in the error is inside src or testsYour own files are not transformed by BabelFix
    2. 2The path in the error starts with /node_modules/A dependency in node_modules ships ES modulesFix
    3. 3package.json has "type": "module" and test files use importThe project is native ESM and Jest runs in CommonJS modeFix

What does the error look like?

  • SyntaxError: Cannot use import statement outside a module
  • Details: /node_modules/some-package/index.js:1 import x from 'y'

Cause 1 of 3

Your own files are not transformed by Babel

1Check

Jest uses babel-jest by default, and babel-jest transforms files when a Babel config exists. No config means imports reach Node as written.

ls babel.config.js .babelrc 2>/dev/null; npm ls babel-jest

2Fix

Install Babel and the env preset, and add a config that targets the current Node version. Add the TypeScript preset too for .ts files.

npm install --save-dev babel-jest @babel/core @babel/preset-env

// babel.config.js
module.exports = {
  presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};

3Verify

Run the tests.

npx jest

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 3

A dependency in node_modules ships ES modules

1Check

Jest's default transformIgnorePatterns skips node_modules, so an untranspiled ESM package breaks.

npx jest --showConfig | grep -A3 transformIgnorePatterns

2Fix

Let that one package through the transform with a negative lookahead. pnpm needs the .pnpm form of the path.

// jest.config.js
module.exports = {
  transformIgnorePatterns: ['/node_modules/(?!(some-package)/)'],
};

3Verify

Run the tests; the error moves on or disappears.

npx jest

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

The project is native ESM and Jest runs in CommonJS mode

1Check

Node treats .mjs files, and files under a package with type module, as ESM.

grep -n '"type"' package.json

2Fix

Run Jest with the experimental VM modules flag and disable transforms, or configure the transformer to emit ESM. Import jest from @jest/globals, since it is not a global in ESM.

NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" npx jest

// jest.config.js
export default { transform: {} };

3Verify

Run the tests with the flag.

NODE_OPTIONS=--experimental-vm-modules npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Choose CommonJS-with-Babel or native ESM for the test setup, and do not mix both.
  • Keep babel.config.js at the project root so babel-jest finds it.
  • When adding an ESM-only dependency, check the test run at once.
  • Put the Node.js and Jest versions in the project docs so CI matches.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.