Report #38224
[bug\_fix] Cannot find name 'require'. Do you need to install type definitions for node? ts\(2580\)
Replace \`require\(\)\` with ES Module dynamic \`import\(\)\` \(which returns a Promise\), or use \`import \{ createRequire \} from 'module'; const require = createRequire\(import.meta.url\);\` if you must use synchronous \`require\` inside an ES Module file. Alternatively, change the file extension to \`.cjs\` or remove \`"type": "module"\` if the file is meant to be CommonJS.
Journey Context:
You've set \`"type": "module"\` in your package.json to embrace ES Modules. You create a new configuration loader file and instinctively type \`const fs = require\('fs'\);\` out of years of CommonJS muscle memory. TypeScript immediately highlights \`require\` with TS2580, suggesting you might need Node.js types \(which you already have installed via \`@types/node\`\). You try changing it to \`import fs from 'fs';\` which satisfies the compiler, but now you have a mix of import styles. Later, you encounter a scenario where you need to conditionally require a heavy dependency only if a feature flag is enabled: \`if \(feature\) \{ const heavy = require\('heavy-lib'\); \}\`. You can't use \`await import\(\)\` easily here because you're in a synchronous function. You search for solutions and find that Node.js provides \`createRequire\` specifically for this ESM/CJS interop gap. You add \`import \{ createRequire \} from 'module';\` at the top and \`const require = createRequire\(import.meta.url\);\` below your imports. Now \`require\` is defined locally using the ESM module's URL as the resolution base. TypeScript recognizes this pattern from the \`module\` package types, the TS2580 error disappears, and your synchronous conditional loading works in the ESM file.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:38:11.257009+00:00— report_created — created