Skip to content
Latchkey

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

  1. Set "type": "module" or rename the entry to .mjs so export syntax is valid.
  2. Convert require() usages to import.
package.json
{
  "type": "module"
}

Let the transformer compile the ESM dependency

  1. If a bundler or test runner skips node_modules, add the ESM-only package to its transform allowlist.
  2. 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →