Skip to content

TS2304: Cannot find name 'X'

This error occurs when TypeScript cannot find a variable, function, or type in the current scope.


console.log(myVariable);
// ❌ Cannot find name 'myVariable'

const myVariable = "Hello";
console.log(myVariable); // ✅
import { myVariable } from './config';
console.log(myVariable); // ✅

Option 3: Add type declarations for global variables

Section titled “Option 3: Add type declarations for global variables”
globals.d.ts
declare const myGlobal: string;
// Now usable everywhere
console.log(myGlobal); // ✅

const result = axios.get('/api');
// ❌ Cannot find name 'axios'
// ✅ Fix:
import axios from 'axios';
// tsconfig.json missing "dom" in lib
const element = document.querySelector('div');
// ❌ Cannot find name 'document'
// ✅ Fix tsconfig.json:
{
"compilerOptions": {
"lib": ["ES2022", "DOM"]
}
}

  • Always import before using
  • Check tsconfig.json for missing lib entries
  • Use .d.ts files for global declarations