How to import Chart.js with Webpack
Asked Answered
K

4

18

I have a Vue JS project using Webpack and need to import Chart.js. I have tried

// import Chart from 'chart.js/Chart.min.js'

This gives js errors when trying to use the library.

Karlynkarma answered 15/3, 2016 at 23:19 Comment(0)
K
18

After you do npm install chart.js,

You can simply use

import Chart from 'chart.js/auto'

to import the whole of chart.js

If you want to import only some modules/plugins:

import {
  Chart,
  BarController,
  BarElement,
  LinearScale,
  CategoryScale
} from 'chart.js'
Chart.register(
  BarController,
  BarElement,
  LinearScale,
  CategoryScale
)
Karole answered 7/9, 2016 at 22:28 Comment(1)
Have you found a solution? I met similar error. Thanks.Archean
E
4

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
Easley answered 7/7, 2022 at 16:59 Comment(1)
This did the job !Ocreate
F
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.

Francophobe answered 30/3, 2017 at 15:10 Comment(0)
H
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.

Hoban answered 12/7, 2023 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.