angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

Webpack attempts to require Chart.js as 'chart'

Open neilff opened this issue 11 years ago • 9 comments

Module not found: Error: Cannot resolve module 'chart' in /node_modules/angular-chart.js/dist
resolve module chart in /node_modules/angular-chart.js/dist
  looking for modules in /node_modules
    /node_modules/chart doesn't exist (module as directory)
    resolve 'file' chart in /node_modules

Changing the AMD register to use chart.js instead of chart resolves the issue:

  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['angular', 'chart.js'], factory);

Or using webpack to create an alias also resolves the issue:

  config.resolve.alias = {
    'chart': require.resolve('chart.js')
  }

Is this expected behaviour?

neilff avatar Aug 31 '15 14:08 neilff

This behavior was introduced in the last update to fix a requirejs issue

jantimon avatar Aug 31 '15 17:08 jantimon

Yes sorry about that, as @jantimon fixing AMD broke webpack... AMD couldn't be fixed with configuration, whereas webpack does. I should have bumped to major version though but it's a bit too late now. I'm adding an example for webpack, and updating the readme.

jtblin avatar Sep 05 '15 01:09 jtblin

Could we just switch the require calls? then it would work out of the box for Browserify and webpack (requirejs would still work with a configuration like it does now): hacked it together on my mobile do can't test it but it should do:

if (typeof exports === 'object') { // Node/CommonJS module.exports = factory(require('angular'), require('chart.js')); } else if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['angular', 'chart'], factory); } else { // Browser globals factory(angular, Chart); } }

jantimon avatar Sep 05 '15 09:09 jantimon

Ok I'll give it a try.

jtblin avatar Sep 05 '15 09:09 jtblin

Yes that totally worked.

jtblin avatar Sep 07 '15 22:09 jtblin

I am still having this issue with webpack and had to add an alias in webpack for chart.js

krazi3 avatar Sep 05 '16 06:09 krazi3

I had to do:

resolve: { alias: {'chart.js': require.resolve('chartjs')} },

Jorenm avatar Sep 19 '16 21:09 Jorenm

Using Webpack 2 I had this error:

Module not found: Error: Can't resolve 'chart' in '/var/www/my-project/node_modules/angular-chart.js/dist'
 @ /var/www/my-project/~/angular-chart.js/dist/angular-chart.js 22:4-41
 @ multi vendor

I fixed this using:

module.exports = {
  resolve: {
    alias: { 'chart': require.resolve('chart.js') }
  },
  ...
}

tnganth avatar Dec 21 '16 11:12 tnganth

This is still not working as it comes. But, the alias on the resolve section on webpack works in Webpack 3.8.1.

GabrielGil avatar Nov 14 '17 19:11 GabrielGil