This error occurs when TypeScript cannot locate the Node.js type definitions package. It typically happens when using Node.js built-in modules (like process, Buffer, or require) in TypeScript without having @types/node installed as a development dependency.
The '@types/node' package contains TypeScript type definitions for Node.js built-in APIs and globals. When you use Node.js-specific features like process.env, __dirname, Buffer, or require() in TypeScript code, the compiler needs these type definitions to understand what these globals are and provide type checking. Without @types/node installed, TypeScript treats these as unknown identifiers and throws module resolution errors. This package is part of the DefinitelyTyped ecosystem, a community-maintained repository of type definitions for JavaScript libraries that don't ship with their own types. By default, TypeScript automatically includes all packages found in node_modules/@types/ during compilation, making type definitions available globally without explicit imports. The error commonly appears when starting a new TypeScript project, after a fresh clone without running npm install, or when the types configuration in tsconfig.json explicitly excludes @types/node from the compilation.
The most common fix is installing the package using your package manager:
npm install --save-dev @types/nodeFor other package managers:
# Using Yarn
yarn add -D @types/node
# Using pnpm
pnpm add -D @types/nodeAfter installation, verify it appears in package.json under devDependencies:
{
"devDependencies": {
"@types/node": "^20.10.0"
}
}If you cloned a repository or node_modules was deleted, install all dependencies:
rm -rf node_modules package-lock.json
npm installThis ensures @types/node and all other dependencies are properly installed. If using pnpm or yarn:
# pnpm
rm -rf node_modules pnpm-lock.yaml
pnpm install
# Yarn
rm -rf node_modules yarn.lock
yarn installIf you've explicitly set the types array in tsconfig.json, ensure 'node' is included:
{
"compilerOptions": {
"types": ["node", "jest"] // Include node here
}
}By default, TypeScript includes ALL packages from @types automatically. If types is specified, only listed packages are included. If you don't need to restrict types, remove the types array entirely:
{
"compilerOptions": {
// Remove the "types" array to auto-include all @types packages
"moduleResolution": "node"
}
}Ensure typeRoots includes the node_modules/@types directory:
{
"compilerOptions": {
"typeRoots": [
"./node_modules/@types",
"./types" // Custom types folder (optional)
]
}
}If typeRoots is not specified, TypeScript defaults to node_modules/@types, which is usually correct. Only set this if you need custom type definition locations.
Install a version of @types/node that matches your Node.js runtime:
# Check your Node.js version
node --version # e.g., v20.10.0
# Install matching types
npm install --save-dev @types/node@20For common Node.js versions:
# Node.js 20.x
npm install -D @types/node@20
# Node.js 18.x
npm install -D @types/node@18
# Node.js 16.x
npm install -D @types/node@16After installing @types/node, restart your IDE to clear caches:
VS Code:
1. Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux)
2. Type 'Reload Window' and press Enter
Or restart the TypeScript server:
1. Press Cmd+Shift+P / Ctrl+Shift+P
2. Type 'TypeScript: Restart TS Server'
3. Press Enter
WebStorm/IntelliJ:
File → Invalidate Caches → Invalidate and Restart
Version Compatibility: The @types/node versioning follows Node.js major versions. For example, @types/node@20 provides types for Node.js 20.x. Using mismatched versions can lead to type errors when using newer or deprecated APIs. Always align your @types/node version with your runtime Node.js version.
Automatic Type Acquisition: In VS Code with Automatic Type Acquisition enabled, TypeScript may download @types packages automatically. However, this only works in JavaScript projects (.js files), not TypeScript projects (.ts files). For TypeScript, explicit installation via npm is required.
Monorepo Considerations: In pnpm monorepos with strict node_modules hoisting, @types/node must be installed in each workspace that uses Node.js types. pnpm's default hoisting behavior differs from npm/yarn. Consider adding @types/node to the root package.json or using .npmrc with public-hoist-pattern[]=@types/*.
Types vs typeRoots: The types compiler option specifies which packages to include, while typeRoots specifies where to look for type packages. If types is set, TypeScript ONLY loads those specific packages from typeRoots. If types is omitted, ALL packages in typeRoots are included automatically. Most projects should omit both settings and rely on defaults.
ESM vs CommonJS: @types/node includes definitions for both CommonJS (require, module.exports) and ES modules (import/export). The moduleResolution setting in tsconfig.json determines which module system TypeScript expects. Use 'node' for CommonJS or 'node16'/'nodenext' for native ESM support in Node.js 16+.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript