Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Error Codes Worth Knowing

TypeScript error TS2304: Cannot find name

Reference for TypeScript error TS2304 Cannot find name: typos, missing imports, missing lib or @types, and how to declare globals the compiler does not know.

TS2304 says the compiler looked for an identifier and found nothing with that name in scope: no local variable, no import, no global declaration. The message is Cannot find name 'x'. For some well-known names it adds a hint, such as Do you need to install type definitions for node? (TS2580) for process or require, or Do you need to change your target library? (TS2583) for newer built-ins like Promise or Map.

The compiler never guesses: a name must be declared, imported, or provided by a lib or @types package.

TypeScript
console.log(totl); // TS2304 (typo)

Try it

TypeScript A typo and a missing import

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Declaring an ambient global

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Using the right scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The first example shows that for a near miss the compiler reports TS2552 (Cannot find name 'totl'. Did you mean 'total'?), the same problem with a suggestion attached.

Syntax

Cause Fix
Misspelled name Correct it; the editor offers quick fixes.
Missing import Import the function, class or type.
Name is block-scoped and used outside Move the declaration to the outer scope.
Node globals (process, Buffer, require) Install @types/node.
Browser globals (document, window) Add "DOM" to lib.
New built-ins (Promise, Map, Array.prototype.at) Raise target or add the right lib entry.
Value injected by a bundler or script tag Declare it: declare const NAME: Type;.
A type used without importing import type { Name } from "./file.js".

Edge case: a name can exist only as a type (an interface) and be missing as a value, or the reverse; then the message is TS2693 ("only refers to a type") or TS2304 depending on how you use it.

Usage notes

  • Check imports first. Auto-import in your editor fixes most cases after you accept the quick fix.
  • Globals need declarations. A declare statement adds no run-time code; it only tells the compiler the value will exist. Put shared ones in a .d.ts file included by the project.
  • types setting. When compilerOptions.types is set, only the listed @types packages are loaded; add the one you need.
  • Generics. A type parameter is only in scope inside its declaration; using T in an unrelated function gives TS2304.
  • Module files vs scripts. In a file without import or export, top-level declarations are global and clash across files; add export {} to make a module (see moduleDetection).
  • Case matters. Promise and promise, String and string are different names.