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;
11 Amazing New JavaScript Features in ES13
This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.