TS2304: Cannot find name
Fix TypeScript error TS2304 'Cannot find name'. Learn how to declare types, install type definitions, and configure tsconfig.json properly.
What Does This Error Mean?
TypeScript error TS2304 'Cannot find name' means the TypeScript compiler cannot find a declaration for a variable, function, class, or type you are using. This typically happens when you reference something that has not been declared, or when type definitions for an external library are missing.
Common Causes
Using a global variable (window, process, __dirname) without declaring it
Missing @types/package for a JavaScript library
Typo in a variable or function name
Referencing a type or interface that has not been defined
Forgetting to export a type from another file
Missing tsconfig.json or incorrect compilerOptions settings
How to Fix It
Install type definitions for the library
Many libraries need @types/package installed for TypeScript to recognize them.
# Install type definitions npm install --save-dev @types/node npm install --save-dev @types/express npm install --save-dev @types/react # Install everything from DefinitelyTyped for a project npm install --save-dev @types/ # Check if types are available: https://www.npmjs.com/search?q=%40types
Declare global variables
Use declare to tell TypeScript about global variables that exist at runtime.
// For Node.js globals like process, Buffer (install @types/node instead)
// For custom global variables
declare const MY_API_KEY: string
// For window globals
declare global {
interface Window {
myAppConfig: { apiUrl: string }
}
}
// Now you can use them
console.log(window.myAppConfig.apiUrl)Fix tsconfig.json settings
Ensure your tsconfig.json includes the right settings for your project.
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"types": ["node"], // Include global types
"skipLibCheck": true // Skip checking .d.ts files
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}Export and import types correctly
Ensure types are properly exported from their source files and imported where used.
// types.ts
export interface User {
id: number
name: string
}
// user.ts
// ❌ Cannot find name User
const user: User = { id: 1, name: "Alice" }
// ✅ Import first
import type { User } from "./types"
const user: User = { id: 1, name: "Alice" }Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
TS2339: Property does not exist on type
Fix TypeScript error TS2339 'Property does not exist on type'. Learn to type objects correctly, use type assertions, and narrow types with guards.
TS18048: 'x' is possibly 'undefined'
Fix TypeScript error TS18048 'is possibly undefined'. Learn to use optional chaining, nullish coalescing, and strict null checks correctly.
TS7016: Could not find a declaration file for module
Fix TypeScript error TS7016 'Could not find a declaration file for module'. Learn to install @types, create custom .d.ts files, and configure typeRoots.
TS2683: this implicitly has type any
Fix TypeScript error TS2683 'this implicitly has type any'. Learn how to type this in callbacks, use arrow functions, and configure noImplicitThis.
Frequently Asked Questions
What is the difference between Cannot find name and Cannot find module?
TS2304 (Cannot find name) means a specific identifier is not declared. TS2307 (Cannot find module) means the import path or package name for a module cannot be resolved.
Does @types/node cover all Node.js globals?
Yes, installing @types/node provides typings for Node.js globals like process, Buffer, __dirname, __filename, setTimeout, console, and all built-in modules (fs, path, http, etc.).