如何解决需要适当加载程序的 npm 错误?

How to resolve npm error needing an appropriate loader?

我用 vue-chartjs 创建了一个 Vue.js 项目。我尝试重新安装库,但仍然出现此错误:

error  in ./node_modules/chart.js/dist/chart.esm.js
Module parse failed: Unexpected token (6613:12)
You may need an appropriate loader to handle this file type.
|         if (intermediateIndex1 !== startIndex && intermediateIndex1 !== lastIndex) {
|           decimated.push({
|             ...data[intermediateIndex1],
|             x: avgX,
|           });
 @ ./node_modules/vue-chartjs/es/BaseCharts.js 1:0-29
 @ ./node_modules/vue-chartjs/es/index.js

App.vue:

<template>
  <div id="app"></div>
</template>

<script>
import axios from "axios";
import moment from "moment";
import LineChart from "./components/LineChart";

export default {
  name: "App",
  components: {
    LineChart
  },
}

LineChart.vue

<script>
import { Line } from "vue-chartjs";

export default {
  extends: Line,
  props: {
    label: {
      type: String
    },
    chartData: {
      type: Array
    },
    options: {
      type: Object
    },
  },
  mounted() {
    const dates = this.chartData.map(d => d.date).reverse();
    const totals = this.chartData.map(d => d.total).reverse();

    this.renderChart(
      {
        labels: dates,
        datasets: [
          {
            label: this.label,
            data: totals
          }
        ]
      },
      this.options
    );
  }
};
</script>

................................................ ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .....................

很有可能您安装了 chartjs 版本 3。vue 包装器与此版本 chart.js 不兼容,仅支持旧版本 2。

如果您通过将 package.json 中的版本号更改为 2.9.4 和 运行 再次将您的安装命令降级到版本 2.9.4 或删除软件包并使用命令 install chart.js@2.9.4。这很可能会解决您的问题