角材料(8)S2591:找不到名称'require' [英] Angular Material (8) S2591: Cannot find name 'require'
问题描述
我正在尝试将日期/时间记录到javascript控制台中.我收到的错误消息如下,是由下面的代码生成的.
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:该代码确实有效.日期将转到控制台.只是错误消息仍然存在
ETA: the code does work. The dates are going to the console. It is just the Error Message remains
消息:
src/app/kdc/services/customers.api.service.ts(60,9)中的ERROR:错误TS2591:找不到名称"require".您需要安装类型吗节点的定义?尝试
npm i @ types/node
,然后将node
添加到tsconfig中的类型"字段.
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 addnode
to the types field in your tsconfig.
注意:我已经对 tsconfig.json
文件进行了更改,并且还完成了 npm i @ types/node
和 npm i @ types/node--save
运行 npm
时,结果是 3个高安全性漏洞
(请参见下文)
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)
这时我该怎么办?`
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 我在这里找到了消息升级到Angular4后找不到名称"require" 并更改了我的 tsconfig.app.json
文件-可能过大了,但效果很好...
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" ]
},
推荐答案
请确保将类型放在tsconfig.app.json中,而不是tsconfig.json中.
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
然后停止角度cli,然后再次运行
Then stop angular cli then run again
这篇关于角材料(8)S2591:找不到名称'require'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!