This TypeScript error occurs when the compiler cannot find type definitions for the global object. This typically happens in Node.js projects where TypeScript lacks proper type information about global Node.js variables. Installing @types/node or configuring your tsconfig.json correctly resolves this issue.
The "Cannot find name global" error means TypeScript doesn't have type definitions for the Node.js global object. In Node.js, the global object is a special object that contains all global variables and functions (like console, process, Buffer). TypeScript's type checker needs these definitions to understand what properties are available on the global scope. Without the proper type definitions, TypeScript treats any reference to "global" as an unknown identifier, similar to misspelling a variable name.
The most common fix is to install the official Node.js type definitions:
npm install --save-dev @types/nodeor with yarn:
yarn add --dev @types/nodeThis package provides TypeScript with all the type information it needs to understand Node.js global objects and functions.
Ensure your tsconfig.json includes Node.js in the type libraries. Update your compilerOptions:
{
"compilerOptions": {
"lib": ["ES2020"],
"types": ["node"],
"target": "ES2020",
"module": "commonjs"
}
}The key is adding "types": ["node"] or ensuring "lib" includes appropriate Node.js support. If you're running both server and browser code, you might need multiple entries:
{
"compilerOptions": {
"lib": ["ES2020", "DOM"],
"types": ["node"]
}
}If you're adding your own custom global variables, create a declaration file. Create a file like src/global.d.ts:
declare global {
var myCustomGlobal: string;
var myConfig: { apiUrl: string; timeout: number };
}
export {};Then use it in your code:
// This will now work without TS2304 error
myCustomGlobal = "hello";
console.log(global.myConfig.apiUrl);Important: The export {}; at the end is required to mark the file as an external module, which allows augmenting the global scope.
In Node.js 12+ and modern TypeScript, prefer globalThis over global. It's more portable and works across browsers and Node.js:
// Instead of:
global.myVariable = "value";
// Use:
globalThis.myVariable = "value";globalThis is the standardized way to access global scope and doesn't require additional type declarations. If you still get an error with globalThis, install @types/node as shown in Step 1.
Node.js v16+ deprecated NodeJS.Global in favor of globalThis. If you're seeing this error in an old project, upgrading @types/node to the latest version may change how types are exported. For ts-node specifically, you can also use a triple-slash reference in your declaration file: /// <reference types="node" /> at the top of files that need global declarations. When using build tools like webpack or esbuild with Node.js code, ensure your tsconfig target is set to "node" or "commonjs" rather than "web" or "es2015 module" modes. In monorepos, each package may need its own tsconfig.json with proper type configuration. If you're extending the global object with custom properties for type safety, avoid modifying it at runtime without declaring it to TypeScript firstβthis prevents both type errors and runtime surprises.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript