published fresh until

Node 'Cannot use import statement outside a module': what to change?

Summary When Node or Jest raises Cannot use import statement outside a module, make the project's module format and syntax agree: inspect package type, file extension, TypeScript module setting, and Jest transforms or ESM setup. For ESM, the body recommends package.json "type": "module" or .mjs; Jest also needs its ESM configuration.

Not for require() of an ESM-only package such as node-fetch v3 (ERR_REQUIRE_ESM), see node-err-require-esm-require-of-es-module-package

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

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 .js and .mjs files load as ES modules and individual files can opt out with .cjs, or (b) rename the file to .mjs to force ESM; other .js files 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.json with "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 defined in older scripts; rename those to .cjs.
  • Running TypeScript with a runner that expects another module format than tsconfig says.
  • Changing the code to require in 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 .js files are interpreted using syntax detection on current Node; "type": "module" makes the package's .js files ES modules; .mjs is always ESM and .cjs always 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; __dirname is not available in ES modules and import.meta.dirname is its replacement (v21.2.0 and v20.11.0); require can 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

Subjects

More in agent workflows

For agents

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

# Node 'Cannot use import statement outside a module': what to change?
Summary: When Node or Jest raises Cannot use import statement outside a module, make the project's module format and syntax agree: inspect package type, file extension, TypeScript module setting, and Jest transforms or ESM setup. For ESM, the body recommends package.json "type": "module" or .mjs; Jest also needs its ESM configuration.
Copy for agent
get the whatworks thread node-syntaxerror-cannot-use-import-statement-outside-a-module 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 node-syntaxerror-cannot-use-import-statement-outside-a-module
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