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中的可选链操作符(?.),这导致无法正确解析这段代码。

按照提供的步骤尝试解决:

步骤如下:

  1. 安装babel和相关包:
npm install --save-dev @babel/core @babel/cli @babel/preset-env
  1. 创建一个.babelrc文件并添加以下内容:
{
  "presets": ["@babel/preset-env"]
}
  1. 编辑您的js文件,比如yourfile.js,确保包含以下改进后的代码:
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine(); // R = u1⋅G + u2⋅P
  1. 使用babel-cli将代码转换成ES5语法:
npx babel yourfile.js --out-file compiledfile.js
  1. 运行转换后的代码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);
});

成功!

  • 29
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装 Node.js,可以在官网下载对应的版本:https://nodejs.org/en/download/ 接着,可以使用 npm(Node.js 的包管理工具)安装 GanacheWeb3.js: ``` npm install -g ganache-cli web3 ``` 安装完成后,可以在命令行中执行以下命令启动 Ganache: ``` ganache-cli ``` 这会在本地启动一个以太坊仿真节点,你可以在浏览器中访问 http://localhost:8545 来查看节点信息。 接下来,可以使用 Web3.js 连接到 Ganache 节点,并在其中部署和测试 Solidity 智能合约。例如,可以使用以下代码连接到 Ganache 节点: ``` const Web3 = require('web3'); const web3 = new Web3('http://localhost:8545'); ``` 然后,可以使用 web3.js 提供的接口编写 Solidity 智能合约,并使用以下代码将其部署到 Ganache 节点上: ``` const fs = require('fs'); const solc = require('solc'); const contractSource = fs.readFileSync('MyContract.sol', 'utf8'); const compiledContract = solc.compile(contractSource, 1); const contractABI = JSON.parse(compiledContract.contracts[':MyContract'].interface); const contractBytecode = compiledContract.contracts[':MyContract'].bytecode; const MyContract = new web3.eth.Contract(contractABI); MyContract.deploy({ data: contractBytecode, arguments: [123, 'hello'] }).send({ from: '0x123...', gas: 1500000, gasPrice: '30000000000000' }).then((newContractInstance) => { console.log(newContractInstance.options.address); }); ``` 这会将编写的 MyContract 合约部署到 Ganache 节点上,并输出新合约实例的地址。 最后,可以使用 Web3.js 提供的接口与部署的合约进行交互,例如: ``` MyContract.methods.myFunction().call().then((result) => { console.log(result); }); ``` 这会调用 MyContract 合约中的 myFunction 函数,并输出返回结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值