javascript - 有 Angular Material (8) S2591 : Cannot find name 'require'

标签 javascript node.js angular typescript ecmascript-6

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

预计到达时间:该代码确实有效。日期将发送到控制台。只是错误消息仍然存在

消息:

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.

注意:我已经对 tsconfig.json 文件进行了更改,并且还完成了 npm i @types/nodenpm i @types/node --save 运行npm时,结果是3个高安全漏洞(见下文)

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 );
     } 

预计到达时间 我在这里找到了这条消息Cannot find name 'require' after upgrading to Angular4并对我的进行了更改 tsconfig.app.json 文件 - 它可能有点矫枉过正,但它有效......

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

最佳答案

确保将类型放入 tsconfig.app.json 而不是 tsconfig.json

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

还要确保组件中的系统税如下所示

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

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

关于javascript - 有 Angular Material (8) S2591 : Cannot find name 'require' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237785/

相关文章:

angular - 如何直接从 Angular 应用程序上传Azure Blob存储中的大文件

javascript - 如何在 Javascript 中比较对象数组内的对象数组并删除重复的对象?

javascript - 使用 d3.forcesimulation() 的 D3 selfnode 链接

forms - 用 express 提交表格的首选方式是什么?

node.js express.js 根路由重定向到//

Angular 2 路由器 : multiple route parameters

javascript - Angularjs ui-select (select2) 不使用 'Controller as' 语法没有被选中

javascript - Unicode字符(向下三 Angular 形)显示为奇怪的文本

node.js - 如何使用 node-addon-api 保存回调供以后使用?

javascript - 专注于绝对元素