Report #82918
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... not supported
Replace the require\(\) call with a dynamic import\(\). For synchronous usage in CommonJS, refactor to an async function and use const \{ default: pkg \} = await import\('esm-only-package'\). Root cause: The target package is pure ESM \(declares 'type': 'module' in its package.json and/or uses .mjs extensions\). Node.js strictly forbids require\(\) of ESM modules to prevent topological ambiguity in the module loader and to ensure that the static analysis boundaries of ES modules are respected.
Journey Context:
You npm install got \(latest version\) into your Express.js server that uses CommonJS require\(\). On startup, the app crashes immediately with 'Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... got not supported'. You check got's package.json and see 'type': 'module'. You consider renaming all your files to .mjs and using 'import' syntax, but that would require refactoring hundreds of files and changing how \_\_dirname is calculated. You check the Node.js ESM documentation and discover that CommonJS modules can dynamically import\(\) ESM modules. You refactor your code from const got = require\('got'\) to async function setup\(\) \{ const \{ default: got \} = await import\('got'\); ... \}. The app starts successfully because dynamic import\(\) can load ESM from within CommonJS, bridging the two module systems without a full migration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:46:18.708835+00:00— report_created — created