Report #16994
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... not supported
Use dynamic import\(\) instead of require\(\) for ES modules, or convert your project to ESM by adding "type": "module" to package.json
Journey Context:
Developer upgrades chalk to v5 or node-fetch to v3 and their existing CommonJS code const chalk = require\('chalk'\) throws ERR\_REQUIRE\_ESM. They learn these packages moved to pure ESM. Attempting to rename the file to .mjs breaks other require\(\) calls in the codebase. The solution is to change the require to await import\('chalk'\), which returns a Promise resolving to the module namespace. This works because dynamic import\(\) can load ES modules from CommonJS files. Alternatively, if the whole project is being modernized, switching to "type": "module" allows using static import syntax everywhere, though this requires converting all .js files to use ESM syntax and updating file extensions for local imports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:14:21.081001+00:00— report_created — created