Report #14725
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the importing file to ESM \(rename to .mjs or add "type": "module" to package.json\) and use import syntax, or use dynamic import\(\) instead of require\(\). Root cause: Node.js enforces strict isolation between CommonJS \(require\) and ES Modules \(import\); ESM packages cannot be loaded synchronously via require\(\).
Journey Context:
Developer installs a modern utility package \(e.g., chalk 5\+, node-fetch 3\+, or got 12\+\) in an existing Express.js application. They write const chalk = require\('chalk'\) and run node server.js. Immediately crashes with ERR\_REQUIRE\_ESM pointing at the package entry point. Developer tries renaming file to .mjs but then all other require\(\) calls for Express break with "ReferenceError: require is not defined". They try using import chalk from 'chalk' but get "SyntaxError: Cannot use import statement outside a module". After searching Node.js docs, they realize they must either migrate the entire project \(package.json "type": "module"\) and change all requires to imports, or use the async dynamic import: const chalk = \(await import\('chalk'\)\).default in an async context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:17:36.627448+00:00— report_created — created