【问题标题】:Cannot find name 'require' after upgrading to Angular4升级到 Angular4 后找不到名称“require”
【发布时间】:2017-08-23 13:36:29
【问题描述】:

我想在我的 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”中设置了“前缀”:“”。会不会是因为从 'angular-cli.json' 变成 '.angular-cli.json' 的原因?

【问题讨论】:

标签: angular angular-cli


【解决方案1】:

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

import * as Chart from 'chart.js';

【讨论】:

    【解决方案2】:

    问题(如typescript getting error TS2304: cannot find name ' require' 中所述)是未安装节点的类型定义。

    预计生成with @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" ]
    ...
    

    如果我遗漏了什么,请写下评论,我会编辑我的答案。

    【讨论】:

    • 对我没用...我需要安装其他东西吗?
    • 对我来说也不起作用。详情在这里:stackoverflow.com/questions/44421367/types-not-found
    • 注意:你必须在src文件夹下更改tsconfig.app.json添加"types": ["node"],它不在根tsconfig中
    • 对我也不起作用。另一个建议添加 declare var require: any; 的答案令人惊讶。
    • 我错过了第 2 步。谢谢!!
    【解决方案3】:

    我终于找到了解决方案,检查我的 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;

    【讨论】:

    • 我在 polyfills.ts 文件中遇到了这个问题,但是你的“declare var require: any;”修复。谢谢
    【解决方案4】:

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

    因此无法解析 tsconfig.json 的 typeRoots 属性内的 node_modules/@types 文件夹的路径

    所以只需从

    更新路径
    "typeRoots": [
      "../node_modules/@types"
    ]
    

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

    仅确保从 tsconfig.json 的新位置解析 node_modules 的路径

    【讨论】:

      【解决方案5】:

      就我而言,使用 VSCode 和 Angular 5,只需将“节点”添加到 tsconfig.app.json 中的类型。 保存并重新启动服务器。

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

      一个奇怪的事情是,这个问题“找不到require(”,用ts-node执行时不会发生

      【讨论】:

      • 对我来说就像 Angular 6 中的魅力一样。
      • 这个解决方案在 Angular 6 库中对我有用。我需要将"types": [ "node" ], 添加到tsconfig.lib.json
      • 并重新启动服务器。我的天,一直都是这样。在 Angular 9 上工作。?
      【解决方案6】:

      我加了

      "types": [ 
         "node"
      ]
      

      在我的 tsconfig 文件中,它对我有用 tsconfig.json 文件看起来像

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

      【讨论】:

      • 虽然此代码可能会回答问题,但您仍然可以考虑添加一些解释性句子,因为这会增加您的答案对其他用户的价值。
      【解决方案7】:

      适用于 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
      【解决方案8】:

      在给出错误的文件顶部添加以下行:

      declare var require: any
      

      【讨论】:

        猜你喜欢
        • 2019-10-26
        • 2017-09-14
        • 2018-01-03
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2018-07-23
        • 2018-05-10
        • 2016-03-01
        相关资源
        最近更新 更多