Cannot find name
TypeScript cannot find a reference to an identifier in the current scope.
- 1A typo in a variable, function, or type name.
- 2Attempting to use a variable from a module that has not been imported.
- 3Using a variable outside of the block or function in which it was defined.
A variable is used without being declared.
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.
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 { 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.
const msg = "Hello!"; console.log(msg);
Why this works
Ensuring the spelling of the identifier matches its declaration.
✕ Declare the missing name on the global scope
This pollutes the global namespace and can lead to hard-to-debug naming conflicts.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev