Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Can't resolve '@reactchartjs/react-chart-2.js'

I am trying to implement this chart using the module react-chartjs-2 I installed the module:

npm install --save react-chartjs-2 chart.js

So I have this in package.json:

  "chart.js": "^2.9.4",
  "react-chartjs-2": "^2.11.1"

I copied the chart code from github:

import React from 'react'
import { Line } from '@reactchartjs/react-chart.js'

const data = {
  labels: ['1', '2', '3', '4', '5', '6'],
  datasets: [
    {
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      fill: false,
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgba(255, 99, 132, 0.2)',
    },
  ],
}

const options = {
  scales: {
    yAxes: [
      {
        ticks: {
          beginAtZero: true,
        },
      },
    ],
  },
}

const LineChart = () => (
  <>
    <div className='header'>
      <h1 className='title'>Line Chart</h1>
      <div className='links'>
        <a
          className='btn btn-gh'
          href='https://github.com/reactchartjs/react-chartjs-2/blob/react16/example/src/charts/Line.js'
        >
          Github Source
        </a>
      </div>
    </div>
    <Line data={data} options={options} />
  </>
)

export default LineChart

But, I keep getting this error:

Module not found: Can't resolve '@reactchartjs/react-chart-2.js'

Any idea what I did wrong?

like image 297
AG_HIHI Avatar asked Mar 20 '26 17:03

AG_HIHI


2 Answers

I Had The Same Problem , This Worked For Me.

yarn add react-chartjs-2 chart.js

For some reason, npm did not work.

like image 191
Fine Boy Avatar answered Mar 23 '26 06:03

Fine Boy


Just in case someone faces the same error, just do this:

import { Line } from "react-chartjs-2";
like image 32
AG_HIHI Avatar answered Mar 23 '26 07:03

AG_HIHI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!