Report #39987
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Replace the require\(\) call with a dynamic import\(\) expression \(which returns a Promise\), or convert the consuming file to an ES Module by renaming it to .mjs or adding "type": "module" to the nearest package.json.
Journey Context:
A developer installs chalk v5 \(or any pure ESM package\) in an existing CommonJS project. They write const chalk = require\('chalk'\) as they always have. Upon running the script, Node.js throws ERR\_REQUIRE\_ESM. Confused, they check the chalk documentation and see it migrated to ESM. They try changing the require to import chalk from 'chalk' but get a SyntaxError because the file is still treated as CommonJS. They consider renaming the file to .mjs, but that breaks other require calls in the same file. They then learn about dynamic import: const \{ default: chalk \} = await import\('chalk'\). This works because dynamic import is supported in CommonJS modules to load ESM, returning a Promise that resolves to the module namespace. They refactor their async function to use await import\('chalk'\), unblocking the migration while keeping the rest of the codebase intact.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:35:31.927986+00:00— report_created — created