Error Encyclopedia
TS2304

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

1

Using a global variable (window, process, __dirname) without declaring it

2

Missing @types/package for a JavaScript library

3

Typo in a variable or function name

4

Referencing a type or interface that has not been defined

5

Forgetting to export a type from another file

6

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:

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