Skip to content
Latchkey

Jest "Jest encountered an unexpected token" - node_modules Transform

Jest tried to parse a file it never transformed and hit syntax it does not understand - ESM export, JSX, or TypeScript. The file almost always lives in node_modules, which Jest ignores for transforms by default.

What this error means

A suite fails to load with "Jest encountered an unexpected token," pointing at a .js file inside node_modules. The accompanying hint says Jest failed to parse a file using the configured transforms - the dependency ships untranspiled ESM or JSX.

Jest output
Jest encountered an unexpected token

Details:

/app/node_modules/@scope/ui/dist/index.js:1
({"Object.<anonymous>":function(...){export * from './Button';
                                     ^^^^^^
SyntaxError: Unexpected token 'export'

Common causes

Dependency ships untranspiled ESM/JSX

A package distributes raw ESM or JSX in its dist. Because Jest’s default transformIgnorePatterns skips everything under node_modules, that file is loaded as-is and fails to parse.

Transform does not cover the dependency’s syntax

Even if you transform your own code, the dependency’s JSX/TS is outside your transform globs unless you explicitly opt it in, so its modern syntax is never compiled.

How to fix it

Allow-list the package for transformation

Negate the offending package out of transformIgnorePatterns so Jest runs it through your transform.

jest.config.js
// jest.config.js
module.exports = {
  transformIgnorePatterns: ['/node_modules/(?!(@scope/ui|other-esm-pkg)/)'],
};

Make sure a transform actually handles JS in node_modules

  1. Confirm babel-jest (or ts-jest) is wired up for .js/.jsx files.
  2. Add @babel/preset-react/preset-typescript if the dependency ships JSX or TS.
  3. Re-run with --no-cache once, since Jest caches transform results aggressively.

How to prevent it

  • Keep transformIgnorePatterns updated as ESM/JSX-shipping deps are added.
  • Prefer dependencies that ship a CommonJS or pre-transpiled build.
  • Clear the Jest cache in CI when transform config changes.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →