Report #46243
[bug\_fix] Cannot find name 'require'. Do you need to install type definitions for node? Try \`npm i --save-dev @types/node\`.
Install Node.js type definitions: \`npm install --save-dev @types/node\`. Ensure 'types' in tsconfig.json is not explicitly excluding 'node'.
Journey Context:
Developer starts a new Node.js project with \`npm init -y\` and installs TypeScript. They write a simple script \`src/index.ts\` containing \`const fs = require\('fs'\);\`. Upon running \`npx tsc\`, they are met with multiple 'Cannot find name 'require'' errors. They assume TypeScript understands Node.js globals by default. They check their tsconfig.json and see 'target': 'ES2020' but nothing about Node. They try changing 'module' to 'CommonJS', but the error persists. The rabbit hole leads them to search for 'typescript require not found', revealing that TypeScript needs type definitions for the Node.js runtime environment. The fix is to install \`@types/node\`, which provides global declarations for \`require\`, \`module\`, \`\_\_dirname\`, and other Node-specific globals. This works because TypeScript's compiler only includes lib files based on 'target' and 'lib' options, but runtime-specific globals from Node.js are not part of the ECMAScript standard and must be provided via declaration files in node\_modules/@types.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T08:05:46.313707+00:00— report_created — created