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.
/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.
{
"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
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.