How to fix Cannot find name 'module' in TypeScript

TypeScriptBEGINNERMEDIUMUpdated Dec 8, 2025· 166 views

This error occurs when TypeScript cannot find type definitions for the Node.js `module` global variable. Without the proper @types/node package installed, TypeScript has no knowledge of Node.js globals like `module`, `__dirname`, or `process`. Installing and configuring the correct type definitions resolves this issue.

Quick fix

Fixes Cannot find name 'module'

npm install --save-dev @types/node
  1. Install @types/node package for Node.js type definitions
  2. Verify your tsconfig.json includes Node.js types
  3. If using webpack HMR, install @types/webpack-env
  4. Clean install dependencies to ensure no cache issues
typescript — error
npm install --save-dev @types/node
Cannot find name 'module'
shell
npm install --save-dev @types/node

What this error means

When you use Node.js global variables like `module`, `__dirname`, `process`, or `global` in your TypeScript code, TypeScript needs type definitions to understand what these globals are. The error "Cannot find name 'module'" means TypeScript is seeing a reference to the `module` identifier but cannot find its type definition. This typically happens when working with Node.js projects that need the @types/node package, which provides type definitions for all Node.js built-in globals and modules. Without these type definitions, TypeScript treats `module` as an undefined identifier and throws an error.

Typical symptoms

  • TypeScript error TS2304 in the IDE or compiler: "Cannot find name 'module'"
  • References to `module.exports`, `module.hot`, or other module properties show errors
  • Webpack HMR (Hot Module Replacement) code fails to compile with `module.hot.accept` errors
  • Node.js global variables like `__dirname`, `__filename`, `process`, or `global` are reported as undefined
  • Build fails when trying to compile code that uses Node.js APIs
  • Code works fine at runtime but fails during TypeScript compilation

Common causes

  1. The @types/node package is not installed in your project dependencies
  2. Using webpack HMR features without @types/webpack-env installed
  3. TypeScript configuration is not set to recognize the Node.js type definitions
  4. Working in a monorepo where type definitions are not shared across packages
  5. Upgrading TypeScript or type definition packages without running npm install
  6. TypeScript version mismatch between what your project expects and what is installed

How to fix "Cannot find name 'module'"

1Install @types/node package for Node.js type definitions

Open your terminal in the project root and run:

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

For yarn users:

bash
yarn add --dev @types/node

For pnpm users:

bash
pnpm add --save-dev @types/node

This installs TypeScript type definitions for all Node.js built-in globals and modules.

2Verify your tsconfig.json includes Node.js types

Open your tsconfig.json and ensure it recognizes Node.js types. Add or verify these fields:

json
{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["ES2020"],
    "moduleResolution": "node",
    "types": ["node"]
  }
}

The "types": ["node"] option explicitly tells TypeScript to include the Node.js type definitions. If you omit the types array, TypeScript will include all @types packages automatically. The "moduleResolution": "node" setting tells TypeScript to resolve modules the way Node.js does.

3If using webpack HMR, install @types/webpack-env

If your error specifically involves module.hot.accept() or other webpack HMR features, also install:

bash
npm install --save-dev @types/webpack-env

Then update tsconfig.json to include webpack-env types:

json
{
  "compilerOptions": {
    "types": ["node", "webpack-env"]
  }
}

This provides type definitions for webpack's HMR module interface.

4Clean install dependencies to ensure no cache issues

Sometimes npm or yarn caches can cause issues. Perform a clean install:

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

For yarn:

bash
rm -rf node_modules yarn.lock
yarn install

For pnpm:

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

This removes the cached modules and lock file, then reinstalls everything fresh.

5Reload your IDE or restart the TypeScript language server

After installing type definitions, your IDE may still show the old error. Reload the TypeScript language server:

VS Code:
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
- Type "TypeScript: Restart TS Server" and press Enter

WebStorm/IntelliJ:
- Go to File → Invalidate Caches → Invalidate and Restart

Other editors:
- Simply reload or restart the IDE

This forces the IDE to re-read the installed type definitions.

6Verify the error is resolved by compiling TypeScript

Run the TypeScript compiler to verify the error is fixed:

bash
npx tsc --noEmit

If your project has a build script, run it:

bash
npm run build

If no errors are reported, the issue is resolved. Your code can now safely use module and other Node.js globals with full type safety.

Advanced notes

The @types/node package is maintained by the DefinitelyTyped community and tracks Node.js API changes. When upgrading Node.js versions, you may need to update @types/node to ensure type definitions match your runtime. If you are building a library that runs in multiple environments (browser and Node.js), you can use conditional type definitions with the types or typesVersions field in package.json. For monorepo setups using workspaces, ensure each package has its own node_modules with @types/node installed, or use a shared root node_modules. In TypeScript strict mode, the compiler is more aggressive about type checking, so missing types are caught earlier. If you intentionally want to disable type checking for Node.js globals temporarily, you can use a triple-slash directive like /// <reference types="node" /> in specific files instead of globally configuring tsconfig.json.

Frequently asked questions

What does "Cannot find name 'module'" mean?
js global variables like module, __dirname, process, or global in your TypeScript code, TypeScript needs type definitions to understand what these globals are. The error "Cannot find name 'module'" means TypeScript is seeing a reference to the module identifier but cannot find its type definition. js built-in globals and modules.
How do I fix "Cannot find name 'module'"?
This error occurs when TypeScript cannot find type definitions for the Node.js module global variable. Without the proper @types/node package installed, TypeScript has no knowledge of Node.js globals like module, __dirname, or process. Installing and configuring the correct type definitions resolves this issue.
What causes "Cannot find name 'module'"?
The @types/node package is not installed in your project dependencies Using webpack HMR features without @types/webpack-env installed TypeScript configuration is not set to recognize the Node.js type definitions
Is the "Cannot find name 'module'" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading