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.
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.
Open your terminal in the project root and run:
npm install --save-dev @types/nodeFor yarn users:
yarn add --dev @types/nodeFor pnpm users:
pnpm add --save-dev @types/nodeThis installs TypeScript type definitions for all Node.js built-in globals and modules.
Open your tsconfig.json and ensure it recognizes Node.js types. Add or verify these fields:
{
"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.
If your error specifically involves module.hot.accept() or other webpack HMR features, also install:
npm install --save-dev @types/webpack-envThen update tsconfig.json to include webpack-env types:
{
"compilerOptions": {
"types": ["node", "webpack-env"]
}
}This provides type definitions for webpack's HMR module interface.
Sometimes npm or yarn caches can cause issues. Perform a clean install:
rm -rf node_modules package-lock.json
npm installFor yarn:
rm -rf node_modules yarn.lock
yarn installFor pnpm:
rm -rf node_modules pnpm-lock.yaml
pnpm installThis removes the cached modules and lock file, then reinstalls everything fresh.
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.
Run the TypeScript compiler to verify the error is fixed:
npx tsc --noEmitIf your project has a build script, run it:
npm run buildIf no errors are reported, the issue is resolved. Your code can now safely use module and other Node.js globals with full type safety.
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.
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