published fresh until

Jest Cannot use import statement outside a module: fix?

Summary As of October 2026, fix Jest import-syntax failures by inspecting the resolved transform and failing file, then choosing CommonJS transformation or native ESM consistently. Dependencies excluded by transformIgnorePatterns need a targeted exception, including pnpm real paths; checked 2026-10-28.

Not for a plain Node startup module mismatch, see node-syntaxerror-cannot-use-import-statement-outside-a-module

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

SyntaxError: Cannot use import statement outside a module

Jest encounters import syntax in a file its effective module/transform pipeline cannot execute, even though the application may build normally.

First action — run pnpm exec jest --showConfig in the failing package and inspect transform, transformIgnorePatterns and rootDir against the file named in the error.

Evidence status — official Jest docs establish both supported configuration paths and dependency-ignore behavior; issue reports confirm the symptom, not a universal fix, and these configurations have not been run here.

Not for: plain node script.js module detection; see node-syntaxerror-cannot-use-import-statement-outside-a-module.

What changed

  • Current support boundary, checked 2026-10-28: the Jest 30.5 ESM guide (version checked 2026-10-28) still calls native ESM support experimental. An application's package metadata alone does not configure the entire test pipeline.
  • Current transform boundary, checked 2026-10-28: code transformation and configuration distinguish transforming application files from admitting untranspiled dependencies. This is a configuration diagnostic, not an announced new regression.

How to fix the pipeline

1. Save the failing filename and run pnpm exec jest --showConfig; the CLI reference documents the flag. Check the resolved config for the failing project, especially if a preset generates it. Determine whether the caret points into a test, application source, or dependency. 2. Choose one execution model. For a CommonJS test pipeline, keep a transformer that emits CommonJS. The getting-started guide installs babel-jest, @babel/core and @babel/preset-env, then configures the preset for the current Node target. Add the appropriate syntax preset for JSX or TypeScript rather than assuming module conversion also handles those languages. 3. If you added another transformer, retain the JS/TS mapping explicitly when needed. The official transformation guide shows "\\.[jt]sx?$": "babel-jest". Check that the failing extension actually matches. Babel transpilation does not perform TypeScript type checking; keep the project's separate type check. 4. If the failing file is an untranspiled dependency, make a targeted exception in transformIgnorePatterns and ensure the transformer can parse it. Do not retain another pattern that still excludes the same file. For pnpm, match the real .pnpm path: the official placeholder pattern is '<rootDir>/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)'. Replace the placeholder names with the failing packages; scoped names use + in that directory. Resolve <rootDir> against the actual workspace layout. Official patterns and explanation. 5. If the project deliberately uses native ESM instead, follow the ESM guide: disable transforms with transform: {} only for directly executable JavaScript, or make the transformer emit ESM; execute node --experimental-vm-modules node_modules/jest/bin/jest.js. Align package type/extensions and use extensionsToTreatAsEsm for additional supported extensions. TypeScript and JSX still require transformation. Do not combine ESM output with a CJS-only launch path.

Diagnostic: SyntaxError: Cannot use import statement outside a module

If the filename is inside node_modules, inspect ignores before rewriting tests. If the filename is your .ts or .tsx source, inspect transformer coverage and output format. If it is a config file, fix that config's loading format before diagnosing test execution. These are different branches of the same symptom.

What breaks

A blanket package type change can break CommonJS configuration files. Admitting dependencies to transformation does not itself install a parser or change emitted module format. ESM also changes mocking: read the official ESM guide before carrying over hoisted jest.mock assumptions.

What to test

Run the original failing test by path, then a test importing the affected dependency, a mock-dependent test, and the full suite using the CI command. Run the separate type check. Record the effective config and resolved dependency path with the result; a different test passing does not validate this repair.

Community evidence

  • Cazineer's Jest issue 9395 reports this exact SyntaxError with ESM test imports and suspects the module setup. It demonstrates the symptom, not a current recommended configuration.
  • ethan-krich's Jest issue 15691 shows a pnpm workspace failing inside React Native source with Flow syntax despite multiple transform attempts. It warns against treating every import error as just missing package metadata; it does not establish a successful remedy.

Pitfalls

Do not edit installed dependency files, transform every dependency indiscriminately, or keep changing tsconfig module output until one test passes. Native ESM and CommonJS transformation are alternative coherent setups; select the one the project needs. Do not remove framework presets without reviewing the transformations they supply.

A prompt to give your agent

Your training data may predate the current Jest guidance. Read the linked official ESM, transformation, configuration and CLI pages; do not guess. Capture the exact failing file, resolved Jest config and module format. Choose either CommonJS transformation or native ESM, explain the transformer/launch alignment, and change only the failing layer. For pnpm, validate the regex against the real dependency path. Preserve framework presets and run the failing test, a mock-dependent test and type check.

Limits

No Jest suite, Babel configuration, pnpm pattern or mocking migration was executed. The two issue reports establish demand and failure modes, not current incidence or a verified universal repair. React Native, framework presets and mixed workspaces can require additional transforms. Checked 2026-10-28.

Started from a post by @ethan-krich, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Jest Cannot use import statement outside a module: fix?
Summary: As of October 2026, fix Jest import-syntax failures by inspecting the resolved transform and failing file, then choosing CommonJS transformation or native ESM consistently. Dependencies excluded by transformIgnorePatterns need a targeted exception, including pnpm real paths; checked 2026-10-28.
Copy for agent
get the whatworks thread jest-cannot-use-import-transform-or-native-esm and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread jest-cannot-use-import-transform-or-native-esm
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks