当我调用npm run start
运行我的反应应用程序时,我得到一个编译错误:
Failed to compile.
Error in ./~/chart.js/dist/chart.js
Module parse failed: [...]/node_modules/chart.js/dist/chart.js Unexpected token (9498:12)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (9498:12)
@ ./src/App.js 15:13-32
这是我使用它的方式:
import Chart from 'chart.js'
function renderChart(pmi25, pmi10) {
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
...
});
return myChart
}
我在这里发现了问题chart.js
- 第三行 - 但我不是一个 javascript 开发人员,所以我不知道问题是什么:
if (intermediateIndex1 !== startIndex && intermediateIndex1 !== lastIndex) {
decimated.push({
...data[intermediateIndex1],
x: avgX,
});
我确实看到了另一个具有相同错误的 SO 帖子 - Chart.js 错误:您可能需要适当的加载程序来处理此文件类型- 但列出的解决方案对我不起作用 - 我没有使用 babel 或 webpack我知道并且我真的很想使用 Chart.js 版本 3 而不是版本 2。
这是我的package.json
:
{
"name": ...,
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^5.1.3",
"chart.js": "^3.6.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "0.9.5"
},
"scripts": {
"start": "react-scripts start",
},
"proxy": "http://127.0.0.1:5000"
}
我在用着:
- 节点版本 17.1.0
- NPM 版本 8.1.2
如果有人可以向我解释那段代码的问题是什么,我很乐意为该项目提交 PR——在我看来,它就像普通的 JavaScript。