Report #8879
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the requiring file to ESM by renaming to .mjs or adding 'type': 'module' to package.json, or use dynamic import\('package'\) instead of require\(\), or downgrade the dependency to a CommonJS-compatible version. Root cause: Node.js strictly enforces module systems; packages with 'type': 'module' or .mjs exports cannot be synchronously required by CommonJS require\(\).
Journey Context:
Installed latest version of chalk v5 or node-fetch v3 in an Express app, required it with const chalk = require\('chalk'\), immediately got ERR\_REQUIRE\_ESM. Confused because it worked in older versions. Read package README and realized it switched to pure ESM. Tried renaming file to .mjs, but then all other requires in the file broke. Considered staying on old package version. Eventually refactored the entire application to ESM by setting 'type': 'module' in package.json and converting all require\(\) to import, or used the asynchronous import\('chalk'\) pattern inside an async function to load the ESM module dynamically.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:43:15.121466+00:00— report_created — created