简体   繁体   English

Uncaught ReferenceError:在Angular 2 Webpack全局库安装中未定义require

[英]Uncaught ReferenceError: require is not defined on Angular 2 webpack global library installation

I'm new to Angular 2. I'm using ng2-charts for my angular 2 project. 我是Angular 2的新手,我在Angular 2项目中使用ng2-charts。 ng2-charts requires Chart.js to be embedded in my application header, as such: ng2-charts需要Chart.js嵌入到我的应用程序标头中,如下所示:

<script src="node_modules/chart.js/src/chart.js"></script>

From my index.html I can't access nodes_modules (Error: GET http://localhost:4200/node_modules/chart.js/dist/Chart.js ) . 我无法从index.html访问nodes_modules(错误:GET http:// localhost:4200 / node_modules / chart.js / dist / Chart.js )。 From what I understand it's because node_modules are not 'compiled' into the dist folder. 据我了解,这是因为node_modules没有“编译”到dist文件夹中。

Thus I need to add chart.js as a Global Library Installation (as explained here: https://github.com/angular/angular-cli#global-library-installation ) 因此,我需要将chart.js添加为全局库安装(如下所述: https : //github.com/angular/angular-cli#global-library-installation

When I do this I get "Uncaught ReferenceError: require is not defined". 当我这样做时,我得到“ Uncaught ReferenceError:需求未定义”。 I assume it's because chart.js is being loaded before systemJS and thus does not know 'require'. 我认为这是因为chart.js在systemJS之前加载,因此不知道'require'。 I tryed adding systemJS before chart.js in apps[0].scripts but that does not work either. 我尝试在apps [0] .scripts中的chart.js之前添加systemJS,但这也不起作用。

Here's my angular-cli.json: 这是我的angular-cli.json:

{
"project": {
    "version": "1.0.0-beta.16",
    "name": "poc1-iot-monitor-frontend"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets",
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [
        "../node_modules/systemjs/dist/system.src.js",
        "../node_modules/chart.js/src/chart.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}

How would I go about embedding Chart.js or any other external js library? 我将如何嵌入Chart.js或任何其他外部js库?

I'm using angular-cli: 1.0.0-beta.16. 我正在使用angular-cli:1.0.0-beta.16。 node: 4.4.2. 节点:4.4.2。 npm: 3.10.6. npm:3.10.6。 webpack 2.1.0-beta.22. webpack 2.1.0-beta.22。

Turned out I was linking to the wrong file from my angular-cli.json. 原来我是从我的angular-cli.json链接到错误的文件。 Changing angular-cli.json apps[0].scripts to: 将angular-cli.json apps [0] .scripts更改为:

      ...],
      "scripts": [
        "../node_modules/chart.js/dist/Chart.js"
      ],
      ...

did the trick. 做到了。 Also no need to link from index.html anymore. 同样,也不再需要从index.html进行链接。

Thanks to asotog for pointing me in the right direction. 感谢asotog为我指明了正确的方向。

--- EDIT (BONUS INFO) --- -编辑(奖励信息)-

For those wanting to add external libraries to your tests, add them via the karma.conf.js files array: 对于那些想要将外部库添加到您的测试中的人,请通过karma.conf.js文件数组添加它们:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'angular-cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma')
    ],
    files: [
      { pattern: "node_modules/chart.js/dist/Chart.js", included: true, watched: false },
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['angular-cli']
    },
    remapIstanbulReporter: {
      reports: {
        html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: ['progress', 'karma-remap-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

I have no experience using Chart.js, but per you angular cli config seems you are pointing to the non compiled version of chart js that's why the require errors, you should point to the bundled version. 我没有使用Chart.js的经验,但是根据您的角度cli config,您似乎指向的是Chart js的未编译版本,这就是为什么require错误的原因,您应该指向捆绑的版本。

Based on chart js docs there are some gulp build tasks dedicated to generate the bundle see here https://github.com/chartjs/Chart.js#building-and-testing 根据js图表文档,有一些专门用于生成捆绑包的gulp构建任务,请参见此处https://github.com/chartjs/Chart.js#building-and-testing

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

相关问题 使用Angular 2 / webpack“未捕获的ReferenceError:require未定义” - “Uncaught ReferenceError: require is not defined” with Angular 2/webpack webpack:未捕获的 ReferenceError:未定义要求 - webpack : Uncaught ReferenceError: require is not defined 未捕获的ReferenceError:角度2中未定义require - Uncaught ReferenceError: require is not defined in angular 2 未捕获的ReferenceError:require未定义react + webpack - Uncaught ReferenceError: require is not defined react + webpack React 和 Webpack:“未捕获的 ReferenceError:需要未定义” - React and Webpack: “Uncaught ReferenceError: require is not defined” 未捕获的 ReferenceError:未在 webpack 设置上定义要求 - Uncaught ReferenceError: require is not defined on webpack setup 未捕获的ReferenceError:未定义require-Webpack2 - Uncaught ReferenceError: require is not defined - Webpack2 未捕获的ReferenceError:未定义require Webpack + AngularJS - Uncaught ReferenceError: require is not defined Webpack + AngularJS 未捕获的ReferenceError:require未定义webpack与电报 - Uncaught ReferenceError: require is not defined webpack with telegram 带有Webpack的ReactJS:未捕获的ReferenceError:需求未定义 - ReactJS with Webpack: Uncaught ReferenceError: require is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM