javascriptreactjsreact-chartjs-2

React-chart.js 2 Error in modules but was already installed in the package.json file


I have already installed react chart js 2 but it still have this kind of error

./node_modules/react-chartjs-2/dist/index.modern.js Module not found: Can't resolve 'chart.js'

This is my package.json file enter image description here

BarChart.js

import React from "react";
import { Bar } from "react-chartjs-2";

const state = {
  labels: ["Nausea", "Fever", "Muscle Pain"],
  datasets: [
    {
      label: "1st Dose",
      backgroundColor: "rgba(75,192,192,1)",
      borderColor: "rgba(0,0,0,1)",
      borderWidth: 2,
      data: [65, 59, 80],
    },
    {
      label: "2st Dose",
      backgroundColor: "red",
      borderColor: "rgba(0,0,0,1)",
      borderWidth: 2,
      data: [30, 20, 10],
    },
  ],
};

export default class Barchart extends React.Component {
  render() {
    return (
      <div>
        <Bar
          data={state}
          options={{
            title: {
              display: true,
              text: "Average ",
              fontSize: 20,
            },
            legend: {
              display: true,
              position: "right",
            },
          }}
        />
      </div>
    );
  }
}

And the Homepage:

import BarChart from "../../Components/Charts/BarChart";

const Home = () => {
  return (
    <div>
      <BarChart />
    </div>
  );
};

export default Home;

Solution

  • You need chart.js as well as react-chartjs-2.

    npm install chart.js