TS2304
TypeScriptERRORNotableScopeHIGH confidence

Cannot find name

What this means

TypeScript cannot find a reference to an identifier in the current scope.

Why it happens
  1. 1A typo in a variable, function, or type name.
  2. 2Attempting to use a variable from a module that has not been imported.
  3. 3Using a variable outside of the block or function in which it was defined.
How to reproduce

A variable is used without being declared.

trigger — this will error
trigger — this will error
function greet() {
  console.log(message);
}

expected output

error TS2304: Cannot find name 'message'.

Fix 1

Declare the variable

WHEN The variable is missing a declaration.

Declare the variable
function greet() {
  const message = "Hello, World!";
  console.log(message);
}

Why this works

Declaring the variable within the accessible scope makes it available.

Fix 2

Import the identifier

WHEN The identifier exists in another file or module.

Import the identifier
import { message } from "./config";

function greet() {
  console.log(message);
}

Why this works

Importing makes the external identifier available in the current module.

Fix 3

Correct the typo

WHEN The name is misspelled.

Correct the typo
const msg = "Hello!";
console.log(msg);

Why this works

Ensuring the spelling of the identifier matches its declaration.

What not to do

✕ Declare the missing name on the global scope

This pollutes the global namespace and can lead to hard-to-debug naming conflicts.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors