Jest: SyntaxError: Cannot use import statement outside a module (Jest encountered an unexpected token)
Jest tried to run untransformed ES module syntax — usually from a dependency in node_modules.
Seen on:
React
Meaning
Jest transforms your code with Babel/ts-jest but ignores node_modules by default. ESM-only packages need transformIgnorePatterns exceptions, or use Vitest.
Common causes
- ESM-only dependency (uuid, nanoid, d3, lodash-es)
- Missing Babel/ts-jest config
- CSS/asset imports not mocked
⚡ Quick fix
- Add the package to transformIgnorePatterns exceptions
- Map CSS with moduleNameMapper (identity-obj-proxy)
- Consider Vitest for Vite projects
Detailed fix by platform
React
transformIgnorePatterns: ['node_modules/(?!(nanoid|uuid)/)']
How to diagnose
- File — Which file failed (in node_modules?)
- Config — Transforms configured?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026