簡體   English   中英

required 關鍵字不適用於 angular

[英]required keyword not working with angular

require關鍵字不適用於 angular,引發錯誤:

src/app/app.component.ts(8,16): 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.

我該如何解決? 其實我也用nodejs正確安裝了它,但沒有工作。

  1. 正如其他人所說,您需要require嗎? 使用 ES6 導入。
  2. 但是,如果您確實需要require ,則可以在它之前加上declare const require: any 為我工作。

如果您想在代碼中使用 require ,請按照以下步驟操作

首先安裝這個包

npm i -D @types/node

然后在你的 tsconfig.json

"typeRoots": [
            "node_modules/@types", //add this
        ],

而不是使用:

import { Process } from '@types/node'; 您需要更改tsconfig.json:

{
"compilerOptions": {
...
"typeRoots": ["node_modules/@types"]
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM