Node "SyntaxError: Unexpected token 'export'" in CI - Fix the Parse Error
Unexpected token export means a file containing export statements was parsed as CommonJS. It is the mirror image of the import-outside-a-module error.
What this error means
Node throws SyntaxError: Unexpected token 'export', often from inside node_modules, when a dependency ships ESM source that your CommonJS pipeline tries to parse directly.
node
/home/runner/work/app/app/node_modules/some-esm-lib/index.js:1
export default function () {}
^^^^^^
SyntaxError: Unexpected token 'export'Common causes
An ESM-only dependency parsed as CommonJS
A package publishes only ES module source, but your CommonJS context (or a transformer that ignores node_modules) tries to evaluate it as CommonJS.
Your own file uses export without an ESM context
A .js source file uses export but the nearest package.json does not declare type module.
How to fix it
Run the consuming code as ESM
- Set "type": "module" or rename the entry to .mjs so export syntax is valid.
- Convert require() usages to import.
package.json
{
"type": "module"
}Let the transformer compile the ESM dependency
- If a bundler or test runner skips node_modules, add the ESM-only package to its transform allowlist.
- Re-run so the dependency is downleveled to CommonJS before evaluation.
jest.config.js
// jest.config.js
transformIgnorePatterns: ['node_modules/(?!(some-esm-lib)/)']How to prevent it
- Decide whether the runtime is ESM or CommonJS up front, and configure bundlers and test runners to transform ESM-only dependencies instead of feeding them raw to a CommonJS parser.
Related guides
Node "Cannot use import statement outside a module" in CI - Fix ItFix the Node.js "SyntaxError: Cannot use import statement outside a module" in CI by telling Node to treat th…
Node ERR_REQUIRE_ESM "require() of ES Module" in CI - Fix the InteropFix the Node.js ERR_REQUIRE_ESM error in CI by switching to a dynamic import, pinning a CommonJS-compatible v…
Node --experimental-vm-modules Required for ESM Tests in CI - Enable the FlagFix Jest ESM failures in CI that demand node --experimental-vm-modules by enabling the flag through NODE_OPTI…