Report #62525
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the consuming project to ES Modules by adding "type": "module" to package.json and changing all require\(\) to import, or use dynamic import\(\) \(which returns a Promise\) inside an async function to load the ESM package from CommonJS code.
Journey Context:
Developer updates a dependency like chalk to version 5 or got to version 12. Their existing Node.js application uses const chalk = require\('chalk'\). Upon running, Node throws ERR\_REQUIRE\_ESM. The developer tries renaming the file to .mjs, but then all other require statements in the project throw errors. They try using import chalk from 'chalk' but get syntax errors because the file is treated as CommonJS. They realize the package.json of the installed dependency has "type": "module". After researching, they understand that Node.js enforces a strict boundary: ESM cannot be required synchronously. They either convert their entire application by adding "type": "module" to their package.json \(converting all files to use ES module syntax\) or refactor the import to use dynamic import\('chalk'\).then\(...\) or await import\('chalk'\) inside an async IIFE, which works because dynamic import can load ESM from CJS.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:26:04.556709+00:00— report_created — created