Report #37824
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... from ... not supported. Instead change the require of ... to a dynamic import\(\)
Convert the importing file to an ES Module by adding 'type': 'module' to package.json, or rename the file to use .mjs extension, or replace require\(\) with await import\(\). Alternatively, downgrade the dependency to a CommonJS version \(e.g., chalk@4 instead of chalk@5\). Root cause: Node.js strictly separates CommonJS \(require\) and ES Modules \(import\); attempting to require\(\) an ESM file violates the ES Module specification contract and throws at runtime.
Journey Context:
You npm install chalk@latest in your Express server. You write const chalk = require\('chalk'\) and run node index.js. It immediately crashes with ERR\_REQUIRE\_ESM, pointing at the require\('chalk'\) line. You check node\_modules/chalk/package.json and see 'type': 'module'. You try renaming index.js to index.mjs but then all your other require\(\) calls break. You realize you must either refactor the entire codebase to ESM \(adding 'type': 'module' and changing all requires to imports\) or use dynamic import: const \{default: chalk\} = await import\('chalk'\). You choose the latter, wrapping it in an async IIFE, and the server starts because the dynamic import\(\) can load ESM from CommonJS.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T17:58:01.689395+00:00— report_created — created