Report #3621
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the project to ESM by adding "type": "module" to package.json and changing require\(\) to import statements, or use dynamic import\(\) \(const chalk = await import\('chalk'\)\) which works in CommonJS files. Alternatively, downgrade the dependency to a CommonJS-compatible version \(e.g., chalk v4 instead of v5, node-fetch v2 instead of v3\).
Journey Context:
You npm install the latest version of a popular package like chalk \(v5\+\) or node-fetch \(v3\+\) in an existing Node.js project, then try to require it using const chalk = require\('chalk'\). The process crashes immediately with ERR\_REQUIRE\_ESM, stating that the file is an ES Module and cannot be require\(\)'d. You try renaming the file to .mjs, but then all your other CommonJS requires break. You check the package's documentation and realize the maintainer shipped a pure ESM version that dropped CommonJS support entirely. The debugging reveals that Node.js treats ES Modules and CommonJS as distinct systems with specific interop rules: you cannot require\(\) an ESM module, you can only import it. The resolution path involves either migrating the entire project to ESM \(adding "type": "module" to package.json and converting all requires to imports\), using dynamic import\(\) which returns a Promise and can be used in CommonJS files, or pinning the dependency to the previous major version that still exports CommonJS \(chalk@4, node-fetch@2\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T17:46:00.581418+00:00— report_created — created