Skip to content
Latchkey

TypeScript "TS2304: Cannot find name" - Fix Missing lib/Globals

tsc met an identifier it has no declaration for. Either the name was never imported, it is a global from a lib/types package that is not included, or it is simply misspelled. Unlike TS2307 (a missing module), TS2304 is a missing *name*.

What this error means

Type-checking fails with error TS2304: Cannot find name '<x>', naming the identifier - a missing import (useState), a DOM global (document, fetch) absent from lib, or a typo.

tsc output
src/widget.ts:4:18 - error TS2304: Cannot find name 'document'.

4   const el = document.getElementById('app')
                 ~~~~~~~~

Common causes

Missing import for the name

A value/type used without importing it (e.g. useState without import { useState } from 'react') is an unknown name.

DOM/global lib not included

Browser globals like document, window, and fetch require "lib": ["DOM"]. A config with only ["ES2022"] omits them, so the name is unknown.

Missing @types for an ambient global

A global provided by a types package (e.g. test globals, Node globals) is unknown until that @types/* package is installed and included.

How to fix it

Import the name or include the lib

Add the missing import, or include the lib that provides the global.

tsconfig.json
// tsconfig.json - include DOM for browser globals
{ "compilerOptions": { "lib": ["ES2022", "DOM", "DOM.Iterable"] } }

Install the types for ambient globals

For globals from a types package, install and include it.

Terminal
npm install -D @types/node
# then ensure it's included (auto, or list it in compilerOptions.types)

How to prevent it

  • Include DOM in lib for browser code that uses DOM globals.
  • Install @types/* for any ambient globals you rely on.
  • Run tsc --noEmit in CI so missing names fail before build.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →