Skip to content
Latchkey

Node "ReferenceError: exports is not defined in ES module scope" in CI

The file executes as an ES module, where the CommonJS exports and module.exports objects do not exist. This commonly hits transpiled CommonJS output that lands in an ESM-typed package.

What this error means

A built file or hand-written module fails with "ReferenceError: exports is not defined in ES module scope" at the first exports. or module.exports reference.

node
Object.defineProperty(exports, "__esModule", { value: true });
                       ^

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and
'/app/dist/package.json' contains "type": "module".

Common causes

CommonJS-compiled output in an ESM package

TypeScript or Babel emitted CommonJS (exports.__esModule = true), but "type": "module" makes Node parse those .js files as ESM.

Hand-written module.exports in an ESM file

A file authored with module.exports = ... runs under ESM rules where those identifiers are undefined.

How to fix it

Compile to ESM, not CommonJS

Match the emitted module format to the package type so output uses real export statements.

tsconfig.json
// tsconfig.json
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

Or mark compiled output as CommonJS

If the build emits CommonJS, give the output folder its own package.json declaring CommonJS so Node parses it correctly.

dist/package.json
// dist/package.json
{ "type": "commonjs" }

How to prevent it

  • Keep the compiler module target aligned with the package type field.
  • Use .cjs for files that must stay CommonJS in an ESM package.
  • Verify built output parses under the package type before publishing.

Related guides

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