Jest ESM ⚛️ React

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

  1. Add the package to transformIgnorePatterns exceptions
  2. Map CSS with moduleNameMapper (identity-obj-proxy)
  3. Consider Vitest for Vite projects

Detailed fix by platform

React

  1. transformIgnorePatterns: ['node_modules/(?!(nanoid|uuid)/)']

How to diagnose

  1. File — Which file failed (in node_modules?)
  2. 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.