Report #85949
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the requiring file to ESM \(rename to .mjs or add 'type': 'module' to package.json\), use dynamic import\(\) instead of require\(\), or rename the ESM-only dependency file to .cjs if it should be CommonJS. Root cause: Node.js enforces strict separation between synchronous CJS require\(\) and asynchronous ESM loading per the ECMAScript Modules specification.
Journey Context:
You decide to use the latest version of chalk \(v5\+\) in your Express server. You npm install chalk and add const chalk = require\('chalk'\); at the top of server.js. When you run node server.js, you get ERR\_REQUIRE\_ESM pointing at the chalk package.json which has 'type': 'module'. You try renaming server.js to server.mjs but then all your other require\(\) calls break. You attempt to use .cjs extension but your build pipeline doesn't recognize it. Researching the Node.js ESM documentation reveals that ESM modules cannot be loaded via require\(\) because ESM has a different loading phase \(async vs sync\). The solution is to use dynamic import\('chalk'\) which returns a Promise, allowing CJS to consume ESM asynchronously, or you downgrade chalk to v4 which remains CJS.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:51:10.549262+00:00— report_created — created