Report #91531
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert your package to ESM by adding "type": "module" to package.json and changing require\(\) to import statements, or use dynamic import\(\) to load the ESM package from CommonJS code.
Journey Context:
You npm install chalk@5 \(or any pure ESM package\) into your existing Node.js project. When you run node index.js, you get ERR\_REQUIRE\_ESM. You try changing require\('chalk'\) to import chalk from 'chalk' but then Node throws SyntaxError: Cannot use import statement outside a module. You research and find that Node.js treats .js files as CommonJS by default. The fix requires either renaming files to .mjs, adding "type": "module" to package.json \(which flips the default to ESM\), or keeping your project CommonJS and using the asynchronous dynamic import\('chalk'\) function which can load ESM modules from CJS context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:13:37.369482+00:00— report_created — created