Report #4882
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert your file to an ES Module by renaming it to .mjs, or add "type": "module" to your package.json, then change require\(\) to import statements. Alternatively, use dynamic import\(\): const chalk = \(await import\('chalk'\)\).default; to load the ESM package from CommonJS. The root cause is that the package is now ESM-only and cannot be loaded with require\(\).
Journey Context:
You npm install chalk@5 \(or node-fetch@3\) in your existing Node project that uses const x = require\('x'\). Code does const chalk = require\('chalk'\). Crashes with ERR\_REQUIRE\_ESM. You're confused because 'require has worked for 10 years'. Realizing package.json has no "type": "module" and the package you installed dropped CJS support. You consider downgrading to chalk@4, but instead try adding "type": "module" to package.json and using import chalk from 'chalk'. You realize dynamic import\(\) returns a Promise, so you refactor to use await import\(\) in an async IIFE or use top-level await if your Node version supports it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:14:45.191342+00:00— report_created — created