TS2304

Cannot find name 'X'

๐Ÿ“š Difficulty: Beginner โฑ๏ธ 4 min read ๐Ÿ”„ Updated: Dec 2024

What is TS2304?

TS2304 occurs when TypeScript encounters a name (variable, function, class, type, etc.) that it doesn't recognize in the current scope. This usually means the name hasn't been declared, imported, or is misspelled.

// Example of TS2304 error
console.log(userName); // Error: Cannot find name 'userName'

// Variable was never declared

Common Causes

1. Variable Not Declared

// โŒ Using variable before declaration
console.log(message); // Error!

// โœ… Declare first
const message = "Hello";
console.log(message);

2. Missing Import

// โŒ Using React without importing
const App = () => <div>Hello</div>; // Error: Cannot find name 'React'

// โœ… Add import
import React from 'react';
const App = () => <div>Hello</div>;

3. Typo in Name

const userName = "John";

// โŒ Typo
console.log(username); // Error! (lowercase 'n')

// โœ… Correct spelling
console.log(userName);

4. Missing Type Definitions

// โŒ Using Node.js globals without types
console.log(process.env.NODE_ENV); // Error: Cannot find name 'process'

// โœ… Install @types/node
// npm install --save-dev @types/node

5. Scope Issues

function outer() {
    const localVar = "I'm local";
}

// โŒ Accessing variable outside its scope
console.log(localVar); // Error!

6. Browser/DOM Globals

// โŒ In Node.js or non-browser environment
const element = document.getElementById('app'); // Error: Cannot find name 'document'

// โœ… Add 'dom' to lib in tsconfig.json
// Or check if running in browser
if (typeof document !== 'undefined') {
    const element = document.getElementById('app');
}

Solutions

1. Declare the Variable

// Use let, const, or var
let count = 0;
const API_URL = "https://api.example.com";
var legacyVar = "old style";

2. Add Missing Import

// Import named exports
import { useState, useEffect } from 'react';

// Import default export
import axios from 'axios';

// Import types
import type { User } from './types';

3. Install Type Definitions

# For Node.js
npm install --save-dev @types/node

# For jQuery
npm install --save-dev @types/jquery

# For Express
npm install --save-dev @types/express

4. Update tsconfig.json

{
    "compilerOptions": {
        "lib": ["ES2020", "DOM", "DOM.Iterable"],
        "types": ["node", "jest"]
    }
}

5. Declare Global Variables

// For global variables from scripts
declare var $: typeof jQuery;
declare var google: typeof google.maps;

// Or in a .d.ts file
declare global {
    interface Window {
        myGlobal: string;
    }
}

๐Ÿ’ก Pro Tip

Most IDEs (VS Code, WebStorm) can auto-import missing dependencies. Use the quick-fix feature (Ctrl/Cmd + .) when you see TS2304 to add imports automatically.

Real-World Examples

React with TypeScript

// โŒ Common mistake in React 17+
const App = () => {
    const [count, setCount] = useState(0); // Error: Cannot find name 'useState'
    return <div>{count}</div>;
};

// โœ… Import the hook
import { useState } from 'react';

const App = () => {
    const [count, setCount] = useState(0);
    return <div>{count}</div>;
};

Jest Testing

// โŒ Jest globals not recognized
describe('MyTest', () => { // Error: Cannot find name 'describe'
    it('works', () => {
        expect(true).toBe(true);
    });
});

// โœ… Install and configure Jest types
// npm install --save-dev @types/jest
// Add "jest" to types in tsconfig.json

Environment Variables

// โŒ process.env in browser code
const apiKey = process.env.API_KEY; // Error!

// โœ… For Vite/Next.js, use their specific patterns
// Vite
const apiKey = import.meta.env.VITE_API_KEY;

// Next.js
const apiKey = process.env.NEXT_PUBLIC_API_KEY;

โš ๏ธ Watch Out

If you're seeing TS2304 for types like Promise, Map, or Set, your tsconfig.json might be targeting an older ES version. Update "target" or "lib" to include modern JavaScript features.

Summary

Still Confused by Your Error?

Paste your TypeScript error into our analyzer for instant, detailed explanations.

Try Error Analyzer โ†’