[Chartjs]-How to import Chart.js with Webpack

16👍

After you do npm install chart.js,

You can simply use

import Chart from 'chart.js'

to import chart.js
Hope it helps.

3👍

As seen on this issue, if you use Angular CLI you only need to add this to the scripts array in angular-cli.json:

"../node_modules/chart.js/dist/Chart.bundle.min.js"

Restart your application, live reload won’t do the trick.

2👍

Using webpack, I’ve found that I had to add Chart to the global context like so:

// Ensure the Chart class is loaded in the global context
import Chart from 'chart.js/auto';
window.Chart = Chart

0👍

I was struggling with all those issues as well, but then I found this answer to a follow-up trouble. It is well explained and it solved my import problems.

Leave a comment