Skip to content
Latchkey

Node "SyntaxError: Unexpected token export" in CI

Node parsed a file as CommonJS and hit an export statement, which is not valid CommonJS syntax. This is the export-side twin of "Cannot use import statement outside a module".

What this error means

Loading a module fails with "SyntaxError: Unexpected token 'export'", frequently pointing into a node_modules package that shipped raw ESM.

node
/app/node_modules/some-esm-lib/index.js:1
export default function () {}
^^^^^^

SyntaxError: Unexpected token 'export'

Common causes

ESM export syntax in a CommonJS-parsed file

The file uses export but Node treats it as CommonJS because there is no "type": "module" or .mjs extension.

A dependency published untranspiled ESM

A package shipped ESM source that a CommonJS consumer required directly, so Node parses export and throws.

How to fix it

Run the file as an ES module

For your own code, set "type": "module" or use .mjs so export is valid.

package.json
{
  "type": "module"
}

Handle an ESM-only dependency correctly

If a dependency is ESM-only, import it via dynamic import() from CJS, or switch your project to ESM.

index.js
const lib = (await import('some-esm-lib')).default;

How to prevent it

  • Keep the module format consistent across your package.
  • Check whether a dependency ships CJS, ESM, or both.
  • Use dynamic import for ESM-only deps from CommonJS code.

Related guides

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