当前位置:   article > 正文

Web3.providers.HttpProvider is not a constructor报错_typeerror: web3.providers.httpprovider is not a co

typeerror: web3.providers.httpprovider is not a constructor

这个是网上给出的代码,但是一直报错

var localhost = "http://127.0.0.1:7545"
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider(localhost))
web3.eth.getAccounts(function (error, result) {
    console.log("账户列表地址:");
    console.log(result);
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Web3.providers.HttpProvider is not a constructor
在这里插入图片描述
自己也花了很多时间,其实就是版本的原因!我的web版本是4.3.0。只需要修改一步就可以了。

var Web3 = require("web3")
  • 1

改为

const {Web3} = require("web3");
  • 1

完整代码如下图所示

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);
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

结果如图
在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号