Report #66436
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the importing file to an ES Module by renaming it to .mjs, or add 'type': 'module' to package.json and rename CommonJS files to .cjs. Alternatively, use dynamic import\(\) instead of require\(\): const pkg = await import\('package-name'\).
Journey Context:
A developer upgrades chalk from v4 to v5 \(or node-fetch to v3\). They run their existing CommonJS script using require\('chalk'\). Node.js throws ERR\_REQUIRE\_ESM stating that the module is an ES module and cannot be required. The developer tries renaming the file to .mjs but then all their require\(\) calls break with ReferenceError: require is not defined. They realize that Node.js treats files as CommonJS by default \(unless package.json has 'type': 'module'\) and that ES modules cannot be synchronously required. The fix works because dynamic import\(\) returns a Promise and allows loading ES modules from CommonJS, while .mjs or 'type': 'module' opts the file into the ES module system where import syntax is native.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:59:32.700617+00:00— report_created — created