Skip to content
Latchkey

Node "Warning: To load an ES module, set type: module" in CI

Node started parsing a .js file as CommonJS, hit ESM syntax, and printed a performance warning before reparsing it as a module. Declaring the module type removes the warning and the double parse.

What this error means

Output includes "(node:NNN) Warning: To load an ES module, set \"type\": \"module\" in the package.json or use the .mjs extension" followed by a "reparsing as ES module" note.

node
(node:21456) Warning: To load an ES module, set "type": "module" in the package.json or
use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

Common causes

ESM syntax in a .js file with no declared type

Without "type", Node defaults to CommonJS, fails to parse, and falls back to reparsing as ESM with a warning.

A workflow that treats warnings as errors

CI configured to fail on stderr or with --throw-deprecation-style strictness turns the warning into a job failure.

How to fix it

Declare the module type

Set "type": "module" so Node parses the file as ESM the first time with no warning.

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

Or use an explicit extension

Rename ESM entry files to .mjs so Node never guesses the module format.

Terminal
node ./bin/cli.mjs

How to prevent it

  • Always declare "type" in package.json for clarity and speed.
  • Use .mjs/.cjs for files that must be one specific format.
  • Do not rely on the reparse fallback in production scripts.

Related guides

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