How to fix Cannot find name 'module' in TypeScript
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- Install @types/node package for Node.js type definitions
- Verify your tsconfig.json includes Node.js types
- If using webpack HMR, install @types/webpack-env
- Clean install dependencies to ensure no cache issues
npm install --save-dev @types/nodeCannot find name 'module'
npm install --save-dev @types/nodeWhat 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
- 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
- Working in a monorepo where type definitions are not shared across packages
- Upgrading TypeScript or type definition packages without running npm install
- TypeScript version mismatch between what your project expects and what is installed
How to fix "Cannot find name 'module'"
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.
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
- 📚TypeScript Official Documentation
- 📖@types/node npm package(Official Docs)
- 📖TypeScript module declaration files documentation(Official Docs)
- 📖TypeScript modules handbook(Official Docs)
- 📝