Typescript error "Cannot find name 'require'. and Cannot find name 'process'." even when it already has defined the parameter "moduleResolution": "node" into its compiler options.
The following is my current tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"lib": ["es2015", "es6"],
"types": ["reflect-metadata"],
"moduleResolution": "node",
"module": "commonjs",
"rootDir": "src",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"declaration": false
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Type definitions for the Node.js standard library are not bundled by default. You need to install them yourself via the @types/node package:
npm install --save @types/node
The moduleResolution option controls how the compiler handles import statements, and has nothing to do with the Node.js standard library.
To fix the "Cannot find name 'require'" errors, you should also convert any CommonJS imports to use TypeScript import syntax:
// Before
var process = require('process');
// After
import process = require('process');
See the Migrating from JavaScript section of the TypeScript handbook for details.
For me it is resolved by adding types to the angular compiler options in angular 8.
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"types": [ "node" ]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With