角材料(8)S2591:找不到名称'require'

时间:2020-02-15 10:16:40

标签: javascript node.js angular typescript ecmascript-6

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

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

消息:

src / app / kdc / services / customers.api.service.ts(60,9)中的

ERROR:错误 TS2591:找不到名称“ require”。您需要安装类型吗 节点的定义?尝试npm i @types/node,然后将node添加到 tsconfig中的“类型”字段。

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

enter image description here

这时我该怎么办?`

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 我在这里Cannot find name 'require' after upgrading to Angular4找到了邮件,并更改了我的邮件 tsconfig.app.json个文件-可能有些过分,但效果不错...

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

1 个答案:

答案 0 :(得分:5)

确保将类型放在tsconfig.app.json中,而不是tsconfig.json中

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

还要确保组件中的系统税看起来像这样

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

然后停止角度cli,然后再次运行