TypeScript error: Cannot find name ‘window’ or ‘document’

Updated: January 8, 2024 By: Guest Contributor Post a comment

When working with TypeScript, developers may encounter errors that can seem perplexing at first glance. One such common error is the inability for TypeScript to recognize global objects typically found in a browser environment, such as ‘window’ or ‘document’.

This error can be frustrating for developers who are trying to use these global browser objects in a TypeScript file. In this tutorial, we’re going to address the reasons for these errors and provide multiple solutions on how to overcome this problem.

Understanding the Problem

TypeScript works by using definition files (.d.ts files) to understand the shapes and available APIs of various libraries including that of the browser’s global variables. If TypeScript cannot find the appropriate definition file or the project is misconfigured, it would not recognize global variables such as ‘window’ or ‘document’, leading to errors.

Solution 1: Include DOM Library in tsconfig.json

The most straightforward way to resolve the error is ensuring that your project’s tsconfig file includes the “dom” library. This tells TypeScript to include global definitions for standard browser objects.

Steps to implement:

  1. Open the ‘tsconfig.json’ file in your project.
  2. Locate the “compilerOptions” section.
  3. Add “dom” to your “lib” array. If you do not have a “lib” array, you will need to create one and add “dom” to it.
  4. Save the changes and recompile your TypeScript project.

Code example:

{
  "compilerOptions": {
    "lib": ["dom", "es6"]
  }
}

Including the DOM does not directly affect performance, but it does broaden the available TypeScript definitions which might result in your IDE or build tools consuming more resources to provide intelligence.

  • Pros: Simple and effective; it directly addresses the cause of the error.
  • Cons: Not applicable if you are working in a non-browser environment that doesn’t have a ‘window’ or ‘document’ object, such as Node.js.

Solution 2: Define Global Variables

If you’re working on an environment-specific project such as a Node.js application, or if for any reason you want to avoid including the “dom” library globally, you may choose to define the missing global variables manually.

Steps to implement:

  1. Create a new ‘.d.ts’ file in your project if you don’t have one. This file can be placed anywhere but commonly it’s located at the root. A common filename is ‘globals.d.ts’.
  2. Declare the global variables that TypeScript fails to infer.
  3. Save the file and ensure it is included in your compilation context (TypeScript should include all ‘.d.ts’ files automatically).

Code example:

declare var window: any;
declare var document: any;

By declaring the variables as ‘any’, you are basically opting out of type checking for these variables. This is a quick fix but loses the advantage of type-safety.

  • Pros: This method allows focused control over what is globally available without relying on a big external definition library.
  • Cons: It removes type checking for the declared variables, potentially leading to runtime errors.

Final Words

Through these and possibly other methods within the ecosystem of TypeScript development, the ‘Cannot find name window or document’ error can be addressed effectively, according to the project’s specific circumstances. Understanding the underlying reason behind these errors and having a clear strategy for debugging can help keep your development process smooth and efficient.