Cannot find name ‘require’ error occurs when you “try to use the require() function, which is a part of the CommonJS module system, but TypeScript is unaware of it.”
How to fix the cannot find name ‘require’ error?
Here is the step-by-step guide to fix the cannot find name ‘ require’ error in TypeScript.
Step 1: Install the Node.js type definitions
You can install the type definitions for Node.js by running the following command:
npm install --save-dev @types/node
This command installs the @types/node package, which contains the type definitions for Node.js APIs, including the require() function.
Step 2: Update your tsconfig.json file
In your tsconfig.json file, add the “node” value to the “types” array in the “compilerOptions” object. This tells TypeScript to include the type definitions for Node.js.
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"types": ["node"],
// ... other options ...
},
// ... other configurations ...
}
If the “types” property is not present in the “compilerOptions” object, add it.
Step 3: Restart your TypeScript compiler or IDE
After making the changes, restart your TypeScript compiler or IDE to ensure the new configurations are applied.
Now, TypeScript should recognize the require() function without any issues.
Use ES6 Imports
If possible, consider using ES6 imports instead of require. For example, instead of:
const fs = require('fs');
Use:
import * as fs from 'fs';
Allow require globally
If you don’t want to use Node.js types but still want to suppress this error, you can tell TypeScript to allow undeclared variables (including require()). Add this to the top of your TypeScript file:
declare var require: any;
This is a less ideal solution because you’re bypassing TypeScript’s type system, but it can be a quick fix in some situations.
Check your tsconfig.json
Ensure that the module option in your tsconfig.json is set to CommonJS if you’re using Node.js:
{
"compilerOptions": {
"module": "CommonJS"
}
}
Also, if your tsconfig.json has the noImplicitAny option set to true, you might get this error if TypeScript doesn’t know about the require() function. The above steps should help in that case.
Check for typos or incorrect paths
Ensure you do not have a typo in the module name or the path you’re trying to require.
I hope this will fix your issues!

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.