-1

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;
JS3
  • 1,623
  • 3
  • 23
  • 52

2 Answers2

2

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

npm install chart.js

enjineerMan
  • 112
  • 1
  • 9
1

Kindly follow these steps to resolve your problem :

  • Delete the 'node_modules' folder from your project and package-lock.json

  • npm install

  • If issue still persists then run

  • npm i react-chartjs-2 or npm i chart.js

  • Or Remove react-chartjs-2 from package.json

  • npm i react-chartjs-2 or npm i chart.js

  • You say to run `npm i react-chartjs-2 or npm i chart.js` - how do you suggest I determine which of the two options to run? I tried your suggestin of deleting node_modules and running npm install again, but this problem persists - and I don't know which of your two suggestions to try from the 4th bullet point – NULL pointer May 19 '23 at 10:00