Report #77549
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module /path/to/package/index.js from /path/to/script.js not supported.
Convert the requiring file to an ES Module by renaming it to .mjs or adding \`"type": "module"\` to your package.json, then use \`import \{ foo \} from 'package'\` syntax. Alternatively, keep the file as CommonJS and use dynamic import: \`const \{ default: chalk \} = await import\('chalk'\);\` which returns a Promise and can load ESM.
Journey Context:
You run \`npm install chalk@5\` \(which is pure ESM\) in an existing CommonJS project that uses \`const chalk = require\('chalk'\)\`. Upon running the script, Node throws ERR\_REQUIRE\_ESM. This occurs because Chalk v5's package.json contains \`"type": "module"\`, marking it as an ES Module. Node.js enforces a strict boundary: ES Modules cannot be loaded synchronously via \`require\(\)\`, which is specific to CommonJS. ES Modules use a different loading, linking, and execution model that supports top-level await and static analysis. The fix works because converting your file to ESM \(via .mjs or "type":"module"\) allows Node to use the ES Module loader which can import other ESM packages. Dynamic import\(\) also works because it returns a Promise and creates an asynchronous execution context that can bridge CJS and ESM.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T12:45:41.000505+00:00— report_created — created