How to fix Cannot find module '@types/node' in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 9, 2025· 306 views

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.

Quick fix

Fixes TS2307

npm install --save-dev @types/node
  1. Install @types/node as a dev dependency
  2. Reinstall dependencies if node_modules is missing
  3. Check tsconfig.json types configuration
  4. Verify typeRoots configuration
typescript — error
npm install --save-dev @types/node
Cannot find module '@types/node'
shell
npm install --save-dev @types/node

What this error means

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.

Typical symptoms

  • TypeScript compiler errors when using process, Buffer, or __dirname
  • Red underlines in IDE on Node.js built-in module imports
  • Error message 'Cannot find name process' or similar for Node globals
  • Build fails with module resolution errors despite Node.js code working at runtime
  • tsconfig.json shows no errors but compilation fails
  • Works in JavaScript but breaks when converting to TypeScript

Common causes

  1. @types/node package not installed in node_modules (most common)
  2. node_modules directory missing or corrupted after git clone
  3. Package listed in package.json but npm install never ran
  4. tsconfig.json types array explicitly set without including 'node'
  5. typeRoots configured incorrectly, excluding node_modules/@types
  6. Using pnpm or yarn without proper hoisting configuration
  7. skipLibCheck enabled hiding the underlying missing types issue
  8. Outdated @types/node version incompatible with TypeScript version

How to fix "Cannot find module '@types/node'"

1Install @types/node as a dev dependency

The most common fix is installing the package using your package manager:

bash
npm install --save-dev @types/node

For other package managers:

bash
# Using Yarn
yarn add -D @types/node

# Using pnpm
pnpm add -D @types/node

After installation, verify it appears in package.json under devDependencies:

json
{
  "devDependencies": {
    "@types/node": "^20.10.0"
  }
}
2Reinstall dependencies if node_modules is missing

If you cloned a repository or node_modules was deleted, install all dependencies:

bash
rm -rf node_modules package-lock.json
npm install

This ensures @types/node and all other dependencies are properly installed. If using pnpm or yarn:

bash
# pnpm
rm -rf node_modules pnpm-lock.yaml
pnpm install

# Yarn
rm -rf node_modules yarn.lock
yarn install
3Check tsconfig.json types configuration

If you've explicitly set the types array in tsconfig.json, ensure 'node' is included:

json
{
  "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:

json
{
  "compilerOptions": {
    // Remove the "types" array to auto-include all @types packages
    "moduleResolution": "node"
  }
}
4Verify typeRoots configuration

Ensure typeRoots includes the node_modules/@types directory:

json
{
  "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.

5Match @types/node version to your Node.js version

Install a version of @types/node that matches your Node.js runtime:

bash
# Check your Node.js version
node --version  # e.g., v20.10.0

# Install matching types
npm install --save-dev @types/node@20

For common Node.js versions:

bash
# 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@16
6Restart your IDE and TypeScript server

After 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

Advanced notes

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+.

Frequently asked questions

What does "TS2307" mean?
js built-in APIs and globals. 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.
How do I fix "TS2307"?
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.
What causes "TS2307"?
@types/node package not installed in node_modules (most common) node_modules directory missing or corrupted after git clone Package listed in package.json but npm install never ran
Is the "TS2307" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading