Skip to content
Advertisement

Angular Material (8) S2591: Cannot find name ‘require’

I am trying to log date/time into the javascript console. The error message I am getting is as follows and was generated by the code below.

ETA: the code does work. The dates are going to the console. It is just the Error Message remains

Message:

ERROR in src/app/kdc/services/customers.api.service.ts(60,9): error TS2591: Cannot find name ‘require’. Do you need to install type definitions for node? Try npm i @types/node and then add node to the types field in your tsconfig.

NOTE: I have already made changes to the tsconfig.json file and have also done npm i @types/node and npm i @types/node --save When running npm result was 3 high-security vulnerabilities (see below)

enter image description here

What can I do at this point?`

customer.api.service.ts

   getCustomers(): Observable<Customers[]> {
        return this.httpclient.get<Customers[]>(this._url)
        .pipe( catchError(this.handleError));
        
    } 

    handleError(error:HttpErrorResponse){
        let rval = Math.random().toString(36).substring(7).toUpperCase();
        require('log-timestamp');
        console.error('MSG NO :' + rval );
        console.error(error);
        return throwError(rval + " <-> " + error.name + " <-> " + error.statusText );
     } 

ETA I found the message here Cannot find name ‘require’ after upgrading to Angular4 and made the change to my tsconfig.app.json file – it may be overkill, but it worked …

  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [ "node" ],
    "typeRoots": [ "../node_modules/@types" ]
  },

Advertisement

Answer

Make sure you put the type in your tsconfig.app.json not your tsconfig.json

  "compilerOptions": {
    "module": "esNext",
    "types": ["node"]
  },

Also make sure your systax in your component look like this

const someLib = require("someLib"); // make sure the name is match with your package name

Then stop angular cli then run again

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement