React-chart.js 2 模块错误但已安装在 package.json 文件中

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

我已经安装了react chart js 2但是还是出现这种错误

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

这是我的 package.json 文件

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>
    );
  }
}

主页:

从“../../Components/Charts/BarChart”导入条形图;

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

export default Home;

您需要 chart.js 以及 react-chartjs-2。

npm install chart.js

请按照以下步骤解决您的问题:

  • 从项目和包中删除 'node_modules' 文件夹-lock.json

  • npm 安装

  • 如果问题仍然存在,那么运行

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

  • 或者从 package.json

    中移除 react-chartjs-2
  • npm i react-chartjs-2 或 npm i chart.js