Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error "Cannot find name 'require'. and Cannot find name 'process'."

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"
  ]
}
like image 535
José Gabriel González Avatar asked Jul 22 '26 11:07

José Gabriel González


2 Answers

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.

like image 112
Lambda Fairy Avatar answered Jul 25 '26 03:07

Lambda Fairy


For me it is resolved by adding types to the angular compiler options in angular 8.

"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"types": [ "node" ]
}
like image 35
Farida Anjum Avatar answered Jul 25 '26 03:07

Farida Anjum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!