Ganache 解决 Could not install from as it does not contain a package.json file Web3 is not constructor
ETH-Ganache 解决 Could not install from “” as it does not contain a package.json file. Web3 is not a constructor等问题
1.安装
首先进行npm安装
报错:
npm ERR! code ENOLOCAL
npm ERR! Could not install from “” as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ub/.npm/_logs/2024-04-28T13_25_17_563Z-debug.log
尝试更新npm: sudo npm install -g install
更新后仍报错;
尝试创建package.json:
Ⅰnpm init --yes
自动写入内容:
Wrote to /home/ub/source/dapp1/ganache/package.json:
{
"name": "ganache",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Ⅱ创建package-lock-only
npm i --package-lock-only
Ⅲ自动修复
npm audit fix --force
Ⅳ验证是否成功:
运行sudo npm install -g -ganache-cli
显示:ganache@1.0.0
added 1 package in 0.058s`
此时运行ganache-cli
,却显示 command not found;
查阅资料显示原因是 环境变量中不存在该命令
我们先查找安装路径 输入 sudo npm install -g ganache-cli
;
但在这一步的操作中,本人的ganache被重新安装,成功打开ganache-cli。
若需要修改路径则使用 sudo vim /etc/profile
命令
添加以下内容:
export GANACHE=/usr/local/node-v8.9.3-linux-x64/bin export GOROOT=/usr/local/go export GOPATH=$GOROOT/bin export PATH=$PATH:$GOPATH:$GANACHE
即可
2.模拟
使用web3js测试
安装: npm install web3 -save
报错:
npm ERR! Response timeout while trying to fetch https://registry.npmjs.org/web3-eth (over 30000ms)
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ub/.npm/_logs/2024-04-28T13_54_17_495Z-debug.log
更换网络后成功
下一步编写测试代码:
用vim编写简单的js文件 vim test.js
内容:
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545') );
console.log(web3.version);
尝试运行时, node test.js
报错:
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine(); // R = u1⋅G + u2⋅P
SyntaxError: Unexpected token .
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (/home/ub/source/dapp1/ganache2/node_modules/@noble/curves/secp256k1.js:8:26)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:789:10)
询问gpt得知,报错的原因是因为在Linux环境中Node.js默认不支持ES6中的可选链操作符(?.),这导致无法正确解析这段代码。
按照提供的步骤尝试解决:
步骤如下:
- 安装babel和相关包:
npm install --save-dev @babel/core @babel/cli @babel/preset-env
- 创建一个.babelrc文件并添加以下内容:
{
"presets": ["@babel/preset-env"]
}
- 编辑您的js文件,比如yourfile.js,确保包含以下改进后的代码:
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine(); // R = u1⋅G + u2⋅P
- 使用babel-cli将代码转换成ES5语法:
npx babel yourfile.js --out-file compiledfile.js
- 运行转换后的代码compiledfile.js:
node compiledfile.js
使用上述代码后仍然报错,但此时问题并非ES语法问题,经查阅后发现而是web3版本不同所致。
问题:Web3 is not a constructor
修改: 测试能否成功打印所有账户
const localhost = “http://127.0.0.1:8545”;
const {Web3} = require(“web3”);
const web3 = new Web3(new Web3.providers.HttpProvider(localhost));
web3.eth.getAccounts().then(function (result) {
console.log(“账户列表地址:”);
console.log(result);
}).catch(function (error) {
console.error(error);
});
成功!
更多推荐
所有评论(0)