TypeScript does not know what require is
error TS2304: Cannot find name 'require'.Short answer
TS2304 on require means TypeScript has no Node.js type declarations in scope. Install @types/node and make sure tsconfig does not exclude it through a types list. If the project runs as an ES module, require does not exist at runtime either, so use import or createRequire.
Fix it fast
1Find out which cause you have
Check whether the Node.js type package is installed and which types your tsconfig allows.
npm ls @types/node npx tsc --showConfig | grep -A3 '"types"'2Match what you see, then jump to its fix
What does the error look like?
error TS2304: Cannot find name 'require'.Cannot find name 'process'.Cannot find name 'module'.
Cause 1 of 3
@types/node is not installed
1Check
The names require, process and module are declared by the Node.js type package, not by TypeScript itself.
ls node_modules/@types2Fix
Install the package as a dev dependency. TypeScript finds packages under node_modules/@types by itself.
npm install --save-dev @types/node3Verify
The compiler passes with no TS2304.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
tsconfig lists types and node is not in it
1Check
When compilerOptions.types is set, only the listed packages add global names. From TypeScript 6.0 the default is an empty list, so node must be named.
grep -n '"types"\|typeRoots' tsconfig.json2Fix
Add node to the list.
{
"compilerOptions": {
"types": ["node"]
}
}3Verify
Compile again.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
The file is an ES module, where require does not exist
1Check
A file is an ES module with a .mjs extension or when package.json has type set to module. Node.js does not provide require there.
grep -n '"type"' package.json2Fix
Prefer import. If you must load a CommonJS file, build require with createRequire.
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const legacy = require('./legacy.cjs');3Verify
Run the compiled file and confirm it starts.
node dist/index.jsConfirmed by nodejs.org
How do I stop it coming back?
- Keep @types/node in devDependencies of every Node.js TypeScript project.
- Match the @types/node major version to the Node.js version you deploy.
- Decide once between CommonJS and ES modules and set it in package.json.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fixBack end
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fix