Jest throws SyntaxError on import in a test or dependency
SyntaxError: Cannot use import statement outside a moduleShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The path in the error is inside src or testsYour own files are not transformed by BabelFix - 2
The path in the error starts with /node_modules/A dependency in node_modules ships ES modulesFix - 3
package.json has "type": "module" and test files use importThe project is native ESM and Jest runs in CommonJS modeFix
- 1
What does the error look like?
SyntaxError: Cannot use import statement outside a moduleDetails: /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-jest2Fix
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 jestConfirmed by jestjs.io
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 transformIgnorePatterns2Fix
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 jestConfirmed by jestjs.io
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.json2Fix
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 jestConfirmed by jestjs.io
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix