Report #16846
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert your file to an ES Module by renaming it to \`.mjs\` or setting \`"type": "module"\` in \`package.json\`, then replace \`require\(\)\` with \`import\` statements. Alternatively, use the dynamic \`import\(\)\` function which can load ESM from CJS, or downgrade the dependency to a CommonJS-compatible version.
Journey Context:
You're writing a simple Node script using the latest version of \`chalk\` or \`lodash-es\`. You write \`const chalk = require\('chalk'\);\`, run \`node script.js\`, and hit a fatal \`ERR\_REQUIRE\_ESM\`. Confused, you check the package—it's marked as a pure ESM package. The rabbit hole: you learn Node.js treats \`.js\` files as CommonJS by default, and ESM cannot be \`require\(\)\`'d synchronously. You try renaming to \`.mjs\` but now all your other \`require\` calls break. You consider using \`import chalk from 'chalk'\` but realize you need to flip the entire package type. After reading the Node.js ESM docs, you understand the interop boundary: either go full ESM \(with \`.mjs\` or \`"type": "module"\`\) or use the async \`import\('chalk'\)\` inside your CJS to load the ESM dynamically. The fix works because it respects the module system's loading constraints.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:49:42.141903+00:00— report_created — created