TypeScript error TS2304: cannot find name 'require' — how to fix in 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: "Your old friend require suddenly became invisible"

\n\n

You are writing in TypeScript, everything is going smoothly, and then — bam! The compiler gives you an icy message: "TS2304: Cannot find name 'require'". Sound familiar? If you are transitioning from JavaScript to TypeScript or working with Node.js projects, you have likely encountered this ghost. In 2025, this error is still in the top of Stack Overflow (over 200,000 views), but the good news is: it is easy to defeat.

\n\n

In this article, we will break down why TypeScript does not see require, which configs need tweaking, and — most importantly — how to write modern code to forget about this problem forever. Let's go!

\n\n

What is TS2304 and why does it appear?

\n\n

The error TS2304: Cannot find name 'require' occurs when TypeScript cannot find the definition of the global function require. In JavaScript (especially in the Node.js environment), require is a native function for importing modules in the CommonJS style. But TypeScript by default does not know about the Node.js environment. It thinks you are working in a browser, where require does not exist.

\n\n

Typical scenario:

\n\n
// app.ts\nconst express = require('express');\n// ❌ TS2304: Cannot find name 'require'\n
\n\n

In 2025, TypeScript (already version 5.x) uses ES modules (import/export) by default. If you try to use require without proper configuration, the compiler will rightly protest.

\n\n

Main causes of the error:

\n
    \n
  • The @types/node package is not installed — TypeScript does not know about Node.js global APIs.
  • \n
  • Incorrect tsconfig.json configuration — missing types: ["node"] or module does not match CommonJS.
  • \n
  • Using outdated syntax — in 2025, it is better to use import instead of require.
  • \n
  • Conflict between CommonJS and ESModules — the project is configured for ES modules, but you are writing code in the CommonJS style.
  • \n
\n\n

Method #1: Installing @types/node (quick solution)

\n\n

The simplest and most common way is to let TypeScript "see" Node.js. To do this, install the types package:

\n\n
npm install --save-dev @types/node\n
\n\n

After that, add to tsconfig.json:

\n\n
{\n  "compilerOptions": {\n    "types": ["node"]\n  }\n}\n
\n\n

Now TypeScript knows about the require function, __dirname, process, and other Node.js global objects. The error will disappear.

\n\n

Pros: fast, works "out of the box".
\nCons: you continue to use outdated CommonJS syntax.

\n\n

Method #2: Configuring moduleResolution and esModuleInterop

\n\n

If you want to use require in a project configured for modern modules, add the following options to tsconfig.json:

\n\n
{\n  "compilerOptions": {\n    "module": "commonjs",\n    "moduleResolution": "node",\n    "esModuleInterop": true,\n    "allowSyntheticDefaultImports": true\n  }\n}\n
\n\n
    \n
  • "module": "commonjs" — explicitly specify that the output code will be in CommonJS format.
  • \n
  • "moduleResolution": "node" — look for modules like Node.js (first in node_modules).
  • \n
  • "esModuleInterop": true — allows importing CommonJS modules via import (and vice versa).
  • \n
  • "allowSyntheticDefaultImports": true — allows default imports from modules without a default export.
  • \n
\n\n

Example of working code:

\n\n
import express = require('express');\n// or even\nconst express                

Blogs

Book Recommendations