Report #4065
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module not supported
Root cause: The package is pure ES Module \(ESM\) with 'type': 'module' in its package.json, and Node.js forbids require\(\)-ing ES modules from CommonJS. The fix is one of: 1\) Rename the file to .mjs to treat it as ESM \(but this requires all imports in that file to use ESM syntax\). 2\) Add 'type': 'module' to package.json to enable ESM for all .js files in the project \(but this breaks require\(\) in those files; you can use .cjs for CommonJS files instead\). 3\) If you must stay in CommonJS \(.js without type:module\), convert the import to a require\(\): const express = require\('express'\), or use dynamic import\(\) which is supported in CommonJS: const \{default: express\} = await import\('express'\).
Journey Context:
Developer installs a popular package like chalk \(v5\+\) or node-fetch \(v3\+\) using npm install. Their existing CommonJS code uses const chalk = require\('chalk'\). When they run the script, Node.js throws 'ERR\_REQUIRE\_ESM: require\(\) of ES Module... not supported'. Developer knows ES6 imports are standard JavaScript and is confused because they work in the browser. They try renaming the file to .mjs and it works, but that breaks their require\(\) calls in other files. They try using .js with 'type': 'module' in package.json, but that breaks other CommonJS files in the project. The rabbit hole involves understanding that Node.js has two module systems: CommonJS \(require/module.exports\) and ES Modules \(import/export\). By default, .js files are treated as CommonJS. To use ES modules, you must explicitly signal it via .mjs extension or 'type': 'module'. The pain point is that you cannot easily mix both in the same file, and switching one file to ESM forces a ripple effect on the entire project's module system.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:45:26.867162+00:00— report_created — created