Report #95783
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the requiring file to ESM by renaming it to .mjs, adding "type": "module" to package.json, or use dynamic import\(\) \(which returns a Promise\) instead of require\(\) to load the ESM-only package.
Journey Context:
Installed the latest version of a popular package like chalk \(v5\+\) or node-fetch \(v3\+\) in a CommonJS project. The code uses const chalk = require\('chalk'\);. Upon running node index.js, Node.js throws ERR\_REQUIRE\_ESM. Checked the package's package.json and saw "type": "module". Realized the package is now pure ESM and cannot be required\(\) because Node.js enforces that ES modules cannot be loaded via require\(\) synchronously. Attempted to change require to import but got SyntaxError: Cannot use import statement outside a module. Realized the entire file needs to be treated as ESM. Renamed the file to .mjs as a quick test, which fixed the import but broke other CommonJS require statements in the same file. Ultimately refactored to use dynamic import const \{default: chalk\} = await import\('chalk'\); inside an async function or converted the whole project to ESM by adding "type": "module" to package.json and converting all requires to imports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:21:20.207629+00:00— report_created — created