Skip to content

TypeScript errorTypeScript: “Cannot find name 'x'” (TS2304, TS2580, TS2582, TS2591)

The compiler stops with Cannot find name 'process' (or 'require', '__dirname', 'describe', 'it', 'document', 'window'), often with a hint such as Do you need to install type definitions for node? TypeScript has no type for that global, so it does not know it exists, even if it works at runtime.

Short answer

Install the type package for the environment: npm install --save-dev @types/node for process, require, Buffer and __dirname, and the test runner’s types (@types/jest, or vitest/globals) for describe and it. Then check tsconfig.json: from TypeScript 6.0, types defaults to an empty list, so add "node" (and your test runner) to it; on older versions a types array also limits which @types packages load; and "lib" needs "DOM" for document and window. For your own variable or a library export, the fix is an import or a typo.

Why TypeScript can’t see the name, and the fix

  1. 01

    The Node.js types are not installed

    “Definitions on DefinitelyTyped are also automatically published to npm under the @types scope.” TypeScript, Type declarations: DefinitelyTyped

    process, require, module, Buffer and __dirname come from Node.js, not from JavaScript itself, so TypeScript needs the Node.js type package to know them. On TypeScript 6 or later, also add "node" to types in tsconfig.json.

    npm install --save-dev @types/node
  2. 02

    A types list in tsconfig leaves the package out

    “If types is specified, only packages listed will be included in the global scope.” TypeScript, TSConfig reference: types

    Before TypeScript 6.0, every installed @types package loaded when types was not set. From 6.0 the default is an empty list, so you list the packages you need. Either way, once types is set only those load, so a package you just installed stays invisible until you add it.

    {
      "compilerOptions": {
        "types": ["node", "jest"]
      }
    }
  3. 03

    Test globals: describe, it, expect

    Jest and Vitest add describe, it and expect as globals at runtime, but TypeScript only knows them from the runner’s types. For Jest install @types/jest. For Vitest, either import them from vitest in each file, or set globals: true in the Vitest config and add "vitest/globals" to types.

    // Vitest, per file
    import { describe, it, expect } from 'vitest';
    
    // or tsconfig.json with globals: true
    "types": ["vitest/globals"]
  4. 04

    document or window in a project without the DOM library

    A lib setting in tsconfig.json without "DOM" removes browser globals such as document, window and fetch types. Add it for browser code. If the code really runs on a server, the error is right: move that code to the browser side.

    {
      "compilerOptions": {
        "lib": ["ES2022", "DOM", "DOM.Iterable"]
      }
    }

Checked against the vendor’s docs on : TypeScript: TSConfig reference (types, lib); TypeScript Handbook: Type declarations.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: TypeScript “Property does not exist on type” · TypeScript “Type is not assignable to type” · “require is not defined”

Questions

More on this error

I installed @types/node and still get the error. Why?

Usually a types array in tsconfig.json that does not list node (on TypeScript 6 or later, types is empty unless you set it), or the editor still using an old TypeScript server. Add "node" to types, then restart the TypeScript server in your editor.

Is “Cannot find name” the same as “Cannot find module”?

No. Cannot find name is a missing global or variable. Cannot find module is an import TypeScript cannot resolve, usually a missing package, a wrong path or missing types for that package.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.