Report #71016
[bug\_fix] ERR\_REQUIRE\_ESM Must use import to load ES Module
Convert the consuming file to ES Modules by either: \(1\) renaming it to \`.mjs\`, \(2\) adding \`"type": "module"\` to the nearest \`package.json\`, or \(3\) using dynamic \`import\(\)\` \(e.g., \`const \{default: pkg\} = await import\('package'\)\`\) which works in both CommonJS and ESM contexts. Do not use \`require\(\)\` on ESM-only packages.
Journey Context:
Developer runs \`npm install got\` \(or \`chalk\` v5\+\) in their Express server. They add \`const got = require\('got'\)\` and start the server. Node crashes with \`Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module\`. Confused, they check \`node\_modules/got/package.json\` and see \`"type": "module"\`. They search the error and learn the package is pure ESM. They try adding \`"type": "module"\` to their project's \`package.json\`, but now all their other \`require\(\)\` calls in \`server.js\` break with \`ReferenceError: require is not defined\`. They consider downgrading to \`chalk@4\` \(CJS version\), but instead refactor \`server.js\` to use dynamic \`import\(\)\`: \`const chalk = \(await import\('chalk'\)\).default\`. This works because dynamic import returns a Promise and is valid in CJS files. They realize ESM enforces strict mode and different resolution rules, requiring full migration planning.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:46:34.307775+00:00— report_created — created