What this thread does
The problem
You run node script.js, or start a server, and get SyntaxError: Cannot use import statement outside a module. Without an explicit "type", modern Node detects static import syntax in ambiguous .js files as ESM; older Node or explicit "type": "commonjs" can parse them as CommonJS. Syntax detection is on by default since v22.7.0 and v20.19.0; retrying an ambiguous file as ESM has a performance cost.
Jest: import or export syntax errors
Under Jest, SyntaxError: Cannot use import statement outside a module and SyntaxError: Unexpected token 'export' are Node/V8 parsing errors surfaced by Jest. First inspect the failing file path: is it application/test code or a dependency under node_modules? Jest's transform configuration maps file patterns to transformers; its ignore rules skip node_modules by default, which can leave dependency syntax untransformed.
For application code in a CommonJS test setup, check that transform matches the failing extension and the transformer emits the intended module format. For an untranspiled dependency, allow that specific package through transformIgnorePatterns and ensure a suitable transform handles it. An ignore exception does not itself transform code; any other matching ignore pattern still prevents transformation. Do not remove all dependency exclusions as a generic fix.
For native ESM, follow Jest's ESM guide: disable transforms with transform: {} or configure them to emit ESM, and launch Node with --experimental-vm-modules. Jest follows package "type" / .mjs activation; use extensionsToTreatAsEsm for other extensions such as .ts or .jsx. Changing package.json alone does not complete these Jest requirements.
Untested experiment: after choosing the transform or native-ESM path, rerun the single failing test with the adjusted runner configuration before applying it across the suite.
What the sources say
- Stack Overflow, "SyntaxError: Cannot use import statement outside a module", asked by user3810626 in 2019.
- The accepted answer by jabacchetta quotes Node's documentation: use Node 13.2.0 or newer, then either (a) add a top-level
"type": "module"to the nearest package.json, so.jsand.mjsfiles load as ES modules and individual files can opt out with.cjs, or (b) rename the file to.mjsto force ESM; other.jsfiles follow their package type and, when ambiguous, syntax detection. - Another answer shows the package.json edit with
"type": "module"placed at the top level, not inside scripts. - A TypeScript-specific answer (by Dr-Bracket) reports that a
tsconfig.jsonwith"module": "esnext"caused it for their setup and switching to"module": "commonjs"fixed it. That is one user's case, not a general rule.
Pick one direction first
The choice is the project's, not the agent's. Decide whether this project is ESM or CommonJS, then make config and syntax agree: either package.json "type": "module" plus import syntax, or CommonJS with require. With ESM, relative imports need file extensions and __dirname does not exist (use import.meta.dirname, available from Node 20.11 and 21.2).
How to describe it to your agent
Node fails with "Cannot use import statement outside a module". Check package.json for "type", the file's extension, the tsconfig "module" setting and the test runner config. Tell me which one disagrees with the import syntax before you change anything, and prefer changing config over rewriting every file.
Mistakes to check
- Setting "type": "module" and then hitting
require is not definedin older scripts; rename those to.cjs. - Running TypeScript with a runner that expects another module format than tsconfig says.
- Changing the code to
requirein one file while the rest of the project is ESM.
Limits
Node's behaviour for require() of ES modules changed in recent versions (no flag needed from v22.12.0 and v20.19.0, as long as the module has no top-level await); see the ERR_REQUIRE_ESM thread for that case.
Sources
- Modules: Packages (Node.js docs), read on 19 Oct 2026. Checked: without a type field, ambiguous
.jsfiles are interpreted using syntax detection on current Node; "type": "module" makes the package's.jsfiles ES modules;.mjsis always ESM and.cjsalways CommonJS whatever the type; the nearest package.json applies; ES modules were unflagged in v13.2.0 (supports the Node 13.2.0 statement the body attributes to the Stack Overflow answer). It also describes retry-as-ESM behaviour for ambiguous files. - Modules: ECMAScript modules, Modules: CommonJS modules and Command-line options (Node.js docs). Checked: ESM relative imports need file extensions;
__dirnameis not available in ES modules andimport.meta.dirnameis its replacement (v21.2.0 and v20.11.0);requirecan be rebuilt with createRequire;require()of ES modules needs no flag from v22.12.0 and v20.19.0 and fails on top-level await; syntax detection default from v22.7.0 and v20.19.0. - The TypeScript anecdote is attributed to Dr-Bracket; it was not reproduced.
Started from a post by @user3810626 (question); jabacchetta (answer), packaged by the editor. Original post