升级到Angular4后无法找到名称'require'

140

我希望在我的Angular项目中使用Chart.js。在以前的Angular2版本中,我一直通过使用“chart.loader.ts”来做到这一点:

export const { Chart } = require('chart.js');

然后在组件代码中我只是:

import { Chart } from './chart.loader';

但是在升级到cli 1.0.0和Angular 4之后,我遇到了错误:"无法找到名称'require'"。

复现错误的方法:

ng new newapp
cd newapp
npm install chart.js --save
echo "export const { Chart } = require('chart.js');" >> src/app/chart.loader.ts
ng serve

在我的 "tsconfig.json" 文件中,我有

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

并且在'node_modules/@types/node/index.d.ts'中有:

declare var require: NodeRequire;

所以我很困惑。

顺便说一下,我经常遇到这个警告:

[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)
尽管我在'.angular-cli.json'中设置了"prefix": "",但是是不是因为从'angular-cli.json'更改为'.angular-cli.json'导致的呢?
结果:

尽管我在'.angular-cli.json'中设置了"prefix": "",但是是否因为从'angular-cli.json'更改为'.angular-cli.json'导致问题?


问题出在你的tsconfig.json文件中。请查看此处的解决方案: https://dev59.com/-10Z5IYBdhLWcg3wrR8r - IndyWill
@IndyWill 谢谢,实际上应该在 src/tsconfig.app.json 文件中。你能写一个答案吗? - Thomas Wang
有关 Electron + Angular 2/4,请参见以下链接: https://dev59.com/-10Z5IYBdhLWcg3wrR8r#47364843 - mihaa123
8个回答

262

问题(正如在typescript 获取错误 TS2304:找不到名称 'require'中所述)是未安装Node的类型定义。

对于使用 @angular/cli 1.x 的项目,具体步骤如下:

步骤1:

使用以下任意一种方式安装@types/node

- npm install --save @types/node
- yarn add @types/node -D

步骤 2: 编辑您的 src/tsconfig.app.json 文件,将以下内容添加到空的"types": []处,该处应该已经存在:

...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...

如果我漏掉了什么,请留下评论,我会编辑我的回答。


12
对我没有起作用,我需要安装其他东西吗? - user7605396
1
对我来说也不起作用。详见:https://stackoverflow.com/questions/44421367/types-not-found - user3471528
63
请在 src 文件夹下修改 tsconfig.app.json,添加 "types": ["node"]",注意不要修改根目录的 tsconfig 文件。 - BrunoLM
12
我也尝试过这个方法,但没有成功。另一个答案建议只需添加 declare var require: any; 却出乎意料地有帮助。 - Paritosh
在 Angular 8 中,我只需将 "types": ["node"] 添加到 tsconfig.app.json 中,就可以正常工作了。 - httpete
显示剩余3条评论

40

可在Angular 7及以上版本中使用


我曾面临同样的问题,当时我正在添加

"types": ["node"]

将根目录下的 tsconfig.json 编辑。

src 文件夹下还有一个 tsconfig.app.json ,我通过添加解决了这个问题。

"types": ["node"]

compilerOptions下添加到tsconfig.app.json文件中


{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": ["node"]  ----------------------< added node to the array
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

简单易懂,解决了我的问题 Angular 12。 - Buchi
sconfig.app.json 文件是我缺少的(Angular 14)。谢谢! - Nexonus

25

终于找到了解决方案,检查我的App模块文件 :

import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';
import 'hammerjs';
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';

import { AppRouting } from './app.routing';
import { AppComponent } from './app.component';

declare var require: any;


export function highchartsFactory() {
      const hc = require('highcharts');
      const dd = require('highcharts/modules/drilldown');
      dd(hc);

      return hc;
}

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRouting,
    BrowserAnimationsModule,
    MaterialModule,
    ChartModule
  ],
  providers: [{
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    }],
  bootstrap: [AppComponent]
})
export class AppModule { }

请注意上述代码中的 declare var require: any;


1
我在 polyfills.ts 文件中遇到了这个问题,但是您的 "declare var require: any;" 解决了它。谢谢。 - Andre

16

仍然不确定答案,但可能的解决方法是

import * as Chart from 'chart.js';

16

对于我来说,使用VSCode和Angular 5,只需要在tsconfig.app.json的types中添加“node”即可。保存并重启服务器。

{
    "compilerOptions": {
    ..
    "types": [
      "node"
    ]
  }
  ..
}

有意思的是,这个“找不到require(”的问题,在使用ts-node执行时不会发生。


1
在我的Angular 6中完美运行。 - Pic Mickael
这个解决方案对我在 Angular 6 库中起作用。但是我需要将 "types": [ "node" ], 添加到 tsconfig.lib.json 中。 - Gus
并重新启动服务器。天哪,原来是这样。正在处理 Angular 9。 - Anders8

4
我添加了。
"types": [ 
   "node"
]

在我的 tsconfig 文件中,它对我很有帮助。我的 tsconfig.json 文件如下所示:

  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": [ 
      "node",
      "underscore"
    ]
  },  

虽然这段代码可能回答了问题,但您仍然可以考虑添加一些解释性的句子,因为这会增加其他用户对您答案的价值。 - MBT

3
在出现错误的文件顶部添加以下行:
declare var require: any

0

我将tsconfig.json文件移动到一个新文件夹中以重新组织我的项目。

因此,它无法解析tsconfig.jsontypeRoots属性内的node_modules/@types文件夹的路径。

所以只需要更新路径即可。

"typeRoots": [
  "../node_modules/@types"
]

"typeRoots": [
  "../../node_modules/@types"
]

只需确保从tsconfig.json的新位置解析到node_modules的路径即可


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接