Report #8147
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module not supported
Use dynamic import\('package'\) instead of require\('package'\), or convert your file to an ES module \(.mjs or 'type': 'module'\). Root cause: Node.js treats .js as CommonJS by default; ESM modules cannot be synchronously required due to the ES module specification's static/dynamic loading differences.
Journey Context:
Installed chalk v5 in a legacy Express server. Required it with const chalk = require\('chalk'\) and got ERR\_REQUIRE\_ESM. Thought renaming the file to .mjs would fix it, but that broke all other CommonJS requires in the project. Tried downgrading to chalk v4 but wanted to use new features. Eventually learned that dynamic import\(\) returns a Promise that resolves the ESM module, allowing it to be loaded into a CommonJS context: const \{default: chalk\} = await import\('chalk'\). This works because dynamic import is asynchronous and compatible with ES module loading semantics.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:44:22.537539+00:00— report_created — created