Angular Material (8) S2591:找不到名称“require”

Cas*_*ils 8 javascript node.js typescript ecmascript-6 angular

我正在尝试将日期/时间记录到 javascript 控制台中。我收到的错误消息如下,是由下面的代码生成的。

ETA:代码确实有效。日期将进入控制台。只是错误消息仍然存在

信息:

src/app/kdc/services/customers.api.service.ts(60,9) 中的错误:错误 TS2591:找不到名称“需要”。你需要为节点安装类型定义吗?尝试npm i @types/node然后添加node到 tsconfig 中的 types 字段。

注意:我已经对tsconfig.json文件进行了更改,并且已经完成npm i @types/node并且npm i @types/node --save运行npm结果是3 high-security vulnerabilities(见下文)

在此处输入图片说明

此时我能做什么?`

客户.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 );
     } 
Run Code Online (Sandbox Code Playgroud)

ETA 我在这里发现消息在升级到 Angular4 后找不到名称“需要”并对 我的tsconfig.app.json文件进行更改 - 这可能有点矫枉过正,但它有效......

  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [ "node" ],
    "typeRoots": [ "../node_modules/@types" ]
  },
Run Code Online (Sandbox Code Playgroud)

Ton*_*Ngo 13

确保你把类型放在你的 tsconfig.app.json 而不是你的 tsconfig.json

  "compilerOptions": {
    "module": "esNext",
    "types": ["node"]
  },
Run Code Online (Sandbox Code Playgroud)

还要确保组件中的 systax 看起来像这样

const someLib = require("someLib"); // make sure the name is match with your package name
Run Code Online (Sandbox Code Playgroud)

然后停止angular cli然后再次运行