Report #66066
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the requiring file to ESM by renaming it with .mjs extension or adding '"type": "module"' to package.json, or use dynamic 'import\(\)' instead of 'require\(\)' to load the ESM package.
Journey Context:
You install the latest version of chalk \(v5\+\) or node-fetch \(v3\+\) and try to require it in your index.js using 'const chalk = require\('chalk'\)'. Node.js immediately throws 'ERR\_REQUIRE\_ESM'. You check the package.json of chalk and see '"type": "module"'. You realize the package is pure ESM and cannot be required. You try renaming your file to index.mjs and changing require to import syntax, but that breaks other CommonJS files in your project. You consider using the createRequire function from 'module' to create a require that can handle ESM, but that's experimental. Finally, you decide to use dynamic import: 'const \{default: chalk\} = await import\('chalk'\)', which works because dynamic import can load ESM modules from CommonJS context. This fixes the error because it satisfies Node's requirement that ESM modules be loaded asynchronously through the ESM loader.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:22:22.051567+00:00— report_created — created