简体   繁体   English

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

[英]Angular Material (8) S2591: Cannot find name 'require'

I am trying to log date/time into the javascript console.我正在尝试将日期/时间记录到 javascript 控制台中。 The error message I am getting is as follows and was generated by the code below.我得到的错误信息如下,是由下面的代码生成的。

ETA: the code does work. ETA:代码确实有效。 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'. src/app/kdc/services/customers.api.service.ts(60,9) 中的错误:错误 TS2591:找不到名称“需要”。 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.尝试npm i @types/node ,然后将node添加到 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)注意:我已经对tsconfig.json文件进行了更改,并且还做了npm i @types/nodenpm i @types/node --save运行npm结果是3 high-security vulnerabilities (见下文)

在此处输入图片说明

What can I do at this point?`此时我能做什么?`

customer.api.service.ts客户.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 ... ETA我在这里发现消息在升级到 Angular4 后找不到名称“要求”并对我的tsconfig.app.json文件进行了更改 - 这可能tsconfig.app.json矫枉过正,但它有效......

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

Make sure you put the type in your tsconfig.app.json not your tsconfig.json确保你把类型放在你的 tsconfig.app.json 而不是你的 tsconfig.json

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

Also make sure your systax in your component look like this还要确保组件中的 systax 看起来像这样

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

Then stop angular cli then run again然后停止angular cli然后再次运行

Nowadays you can use ECMAScript import.现在你可以使用 ECMAScript 导入。 From

const someLib = require("someLib");

to

import * as someLib from 'someLib';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM