Special Sponsor:PromptBuilder— Fast, consistent prompt creation powered by 1,000+ expert templates.
Make your Product visible here.Contact Us

Home/TypeScript Errors/TS2304

TS2304

Cannot find name 'X'

TypeScript TS2304 fires when you reference a name — a variable, function, class, or type — that TypeScript cannot find in scope. The most common causes are missing imports, missing type declarations, or typos.

Why This Happens

The identifier you used hasn't been declared in the current scope. It may not be imported, the file it's defined in isn't included in tsconfig, or you're using a browser/Node global that TypeScript doesn't know about without the right lib setting.

Code That Triggers TS2304

// TS2304 examples

// Missing import
const result = useState(false);
// Error: Cannot find name 'useState'
// Fix: import { useState } from 'react';

// Missing global type
const el = document.getElementById("app");
// Error: Cannot find name 'document'
// Fix: add "lib": ["dom"] to tsconfig.json

// Typo
function calculateTax(price: number) {
  return price * 0.2; // typo: 'price' instead of 'price'
  // Error: Cannot find name 'price'
}

How to Fix TS2304

Option 1: Add the missing import

import { useState } from 'react';
import { MyType } from './types';

Option 2: Add lib to tsconfig for browser/Node globals

// tsconfig.json
{
  "compilerOptions": {
    "lib": ["ES2022", "DOM"],
    "types": ["node"] // for Node.js globals like process, Buffer
  }
}

Option 3: Install missing @types package

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

Frequently Asked Questions — TS2304

What does TS2304 mean?

TS2304 means TypeScript can't find a name you referenced. The most common causes are a missing import statement, a typo in the identifier name, or a missing type declaration for a global.

TS2304 for 'process' or 'Buffer' — how to fix?

Install @types/node: npm install --save-dev @types/node, then add 'node' to the types array in tsconfig.json. These are Node.js globals that TypeScript needs type declarations for.

TS2304 for 'document' or 'window' — how to fix?

Add 'DOM' to the lib option in tsconfig.json: "lib": ["ES2022", "DOM"]. Without DOM in lib, TypeScript doesn't know about browser globals.

TS2304 after adding a new file — why?

The file may not be included in your TypeScript compilation. Check that the file is inside the 'include' paths in tsconfig.json and isn't excluded by 'exclude'.

TS2304 for a React hook — how to fix?

Add the import: import { useState, useEffect } from 'react'. TypeScript reports TS2304 when you use hooks without importing them — they're not global.

Convert JavaScript to TypeScript automatically

Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.

Try the converter →

Other TypeScript Errors

From the blog

View all →