This error occurs when TypeScript does not recognize Node.js global objects like process, require, or global. The issue arises because TypeScript lacks the proper type definitions for Node.js environments. Installing the @types/node package and configuring tsconfig.json correctly resolves this compilation error and allows you to access Node.js globals in your TypeScript code.
TypeScript is a statically-typed language that requires type definitions for all objects and variables you use. Node.js provides global objects like process, global, Buffer, and require that are not part of the standard JavaScript specification. When TypeScript compiles your code, it does not recognize these Node.js globals because it lacks the type information needed. This error (TS2304) indicates that the TypeScript compiler cannot find a name in scope. When accessing Node.js globals without proper type definitions, TypeScript cannot determine what these objects are, so it treats them as undefined names and raises an error during compilation.
Open your terminal and install the official Node.js type definitions package. This provides TypeScript with all the type information needed to recognize Node.js globals.
npm install --save-dev @types/nodeIf you are using Yarn:
yarn add --dev @types/nodeIf you are using pnpm:
pnpm add --save-dev @types/nodeOpen your tsconfig.json file and ensure it includes "node" in the types array. If you don't have a types array, add one.
{
"compilerOptions": {
"lib": ["es2015", "dom"],
"types": ["node"]
}
}Alternatively, if you want to be more explicit about which types to include, you can reference @types/node directly:
{
"compilerOptions": {
"types": ["node", "jest"]
}
}If you omit the "types" array entirely, TypeScript will include all @types packages found in node_modules, which works for most projects.
Your IDE may be caching the old TypeScript compilation results. Restart the TypeScript language server to pick up the new type definitions.
For VS Code:
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
- Search for "TypeScript: Restart TS Server"
- Press Enter
Alternatively, reload the entire window:
- Open the Command Palette
- Search for "Developer: Reload Window"
- Press Enter
For WebStorm/IntelliJ:
- Go to File > Invalidate Caches / Restart
- Choose "Invalidate and Restart"
After installing @types/node and restarting your IDE, you should be able to use Node.js globals without errors. Test by accessing the process object:
// Now this works without TypeScript errors
console.log(process.env.NODE_ENV);
console.log(process.cwd());
console.log(process.version);
// You can also use other Node.js globals
const filepath = require("path").join(__dirname, "file.txt");If you still see errors, verify that @types/node was installed correctly by checking node_modules/@types/node exists.
If you have both Node.js server code and browser/frontend code in the same project, you may need separate tsconfig files. Create a tsconfig.node.json for server-side code:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"lib": ["es2015"],
"types": ["node"]
},
"include": ["server/**/*"],
"exclude": ["node_modules", "dist"]
}Then reference it in your build tool configuration (for example, in tsup, esbuild, or webpack) when compiling server-side code. Keep your main tsconfig.json without Node.js types for frontend code to avoid bundling unnecessary type definitions.
Create React App intentionally excludes @types/node from browser code because process is not available in the browser. If you need to access environment variables in a Create React App project, use the REACT_APP_ prefix:
// Correct for Create React App
const apiKey = process.env.REACT_APP_API_KEY;
// Define in .env file
// REACT_APP_API_KEY=your_key_hereIf you need process in backend code that runs outside Create React App (like a server file or build script), keep that code in a separate folder and use a separate tsconfig.node.json with @types/node included.
In monorepo setups (using Turborepo, Nx, or Lerna), you may need to ensure @types/node is installed in the root workspace or in each package that uses Node.js APIs. Some projects use different strategies: installing @types/node as a regular dependency (not devDependency) in workspace packages that ship Node.js code, or centralizing it in the root package.json. Additionally, if you are bundling code for the browser (via Webpack, esbuild, or Vite), the bundler may provide a polyfilled process object. In that case, you still need @types/node for TypeScript compilation, but the runtime will use the bundler's process polyfill. For strict type safety, you can also use namespace declaration merging to define custom types for process.env properties in a global.d.ts file, allowing IntelliSense for your specific environment variables.
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