To fix the “cannot find name ‘require'” error in TypeScript, install the @types/node
package into your project by running npm i -D @types/node
.
This error can occur when you try to use the Node.js require()
function in a TypeScript file.
You can fix it by running the following command in a terminal window at the root directory of your project:
npm i -D @types/node
If the error persists, try adding "node"
to the types
array in your tsconfig.json
file:
tsconfig.json
{
"compilerOptions": {
"types": [
// ... other types
"node"
],
},
}
Tip
If you’re just doing simple testing, you can quickly resolve this error by defining a require
variable at the top of the TypeScript file:
declare var require: any;
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
Sign up and receive a free copy immediately.