你可能需要一个合适的加载器来处理这种文件类型 - React

You may need an appropriate loader to handle this file type - React

你好,我正在用 React 做区块链,但是当我使用

导入 Web 3 时
import Web3 from 'web3'

它给我这个错误:

./node_modules/web3-eth-accounts/lib/index.js
Module parse failed: Unexpected token (225:12)
You may need an appropriate loader to handle this file type.
|         }
|         return signed({
|             ...tx,
|             chainId: args[0],

我正在使用节点 14.17.1,我使用

创建了它
create-react-app my-app --scripts-version 1.1.5

并且我使用

安装了 web3
npm install web3

这是 tsconfig.json 文件,此文件也显示错误

{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
      "outDir": "./lib",
      "esModuleInterop": true
    },
    "include": [
      "./src"
    ]
   }

和index.js错误所指的代码

return signed({
            ...tx,
            chainId: args[0],
            nonce: args[1],
            networkId: args[2],
            ...args[3] // Will either be gasPrice or maxFeePerGas and maxPriorityFeePerGas
        });

你的项目是用 --scripts-version 1.1.5 创建的,这意味着它的 babel 无法解析 ...,考虑使用更高的 scripts-version。对我来说,我从 1.1.4 升级到 3.2.0