Report #104467
[bug\_fix] ERR\_REQUIRE\_ESM
Change the \`require\(\)\` call to an \`import\(\)\` dynamic import, or convert the consuming file to ESM \(use \`.mjs\` extension or set \`"type": "module"\` in package.json\). Alternatively, if the module you're requiring can be imported in CommonJS, check if it provides a CJS entry point or use \`await import\('module'\)\` inside an async function.
Journey Context:
I was working on a Node.js application that used \`require\('chalk'\)\` but had \`"type": "module"\` in package.json. Node 14\+ throws \`ERR\_REQUIRE\_ESM\` because 'chalk' is an ES module and CommonJS \`require\(\)\` cannot load it synchronously. I first tried changing the require statement to \`import chalk from 'chalk'\`, but then my whole file needed to be converted to ESM. I renamed the file to \`.mjs\` and used \`import\` statements everywhere. After testing, everything worked. The fix is to stop using \`require\(\)\` for ES modules. For a quick fix without converting everything, I used \`const chalk = await import\('chalk'\)\` inside an async function. The root cause: the Node.js module system enforces that ES modules cannot be loaded synchronously via \`require\(\)\`. This design is intentional to support static analysis of imports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-23T20:04:51.950721+00:00— report_created — created