【发布时间】:2021-05-28 03:37:22
【问题描述】:
我正在尝试使用 NextJS 创建一个小型以太坊区块链应用程序。
Import Web3 引起了一些问题
ModuleParseError: Module parse failed: C:\Path\node_modules\web3-core-helpers\lib\formatters.js Unexpected token (296:20)
You may need an appropriate loader to handle this file type.
| // If options !== undefined, don't blow out existing data
| if (options.fromBlock === undefined)
| options = { ...options, fromBlock: 'latest' };
| if (options.fromBlock || options.fromBlock === 0)
| options.fromBlock = inputBlockNumberFormatter(options.fromBlock);
at C:\Path\node_modules\webpack\lib\NormalModule.js:303:19
at C:\Path\node_modules\webpack\lib\NormalModule.js:209:11
at C:\Path\node_modules\loader-runner\lib\LoaderRunner.js:373:3
at iterateNormalLoaders (C:\Path\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
at C:\Path\node_modules\loader-runner\lib\LoaderRunner.js:205:4
at C:\Path\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:70:14
at processTicksAndRejections (internal/process/task_queues.js:75:11)
import Web3 from "web3"; 是根本原因
import Web3 from "web3";
const web3 = new Web3(window.ethereum);
export default web3;
这是我的 package.json
{
"name": "myproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "next dev"
},
"author": "",
"license": "ISC",
"dependencies": {
"@truffle/hdwallet-provider": "^1.2.2",
"fs-extra": "^9.1.0",
"ganache-cli": "^6.12.2",
"mocha": "^8.3.0",
"next": "^4.1.4",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"solc": "^0.4.17",
"web3": "^1.3.4"
}
}
我需要其他模块吗?
我注意到在使用 npx create-next-app 创建应用程序时,相同的导入正在工作,但我想使用特定版本,所以我只在我的节点项目中使用了npm install --save next@4.1.4 react@16 react-dom@16。
【问题讨论】:
-
可能不相关,但有什么理由使用非常旧的 Next.js 版本?