Testrpc

web3.setProvider 不是函式

  • May 29, 2020

我正在嘗試了解我安裝的 web3.js

npm install ethereumjs-testrpc

npm install web3  // version 0.20.6
npm install ganache-cli // version 6.0.3

然後我使用以下程式碼

var TestRPC = require("ethereumjs-testrpc");
var web3 = require("web3");
var ganache = require("ganache-cli");
//~ web3.setProvider(TestRPC.provider());
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); 
//~ var web3 = new web3(web3.givenProvider || ganache.server());

//~ var web3 = new web3(web3.givenProvider || 'ws://some.local-or-remote.node:8546');
//~ web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
//~ web3.setProvider(new web3.providers.HttpProvider('http://geth-node-ip:8545'));
//~ console.log(web3.isAddress("0x8888f1f195afa192cfee860698584c030f4c9db1"))




//~ web3.setProvider(TestRPC.provider());
//~ console.log(web3.version);
//~ console.log(web3.providers);

錯誤說web3.setProvider is not a function

我相信你想要這樣的東西。(注意大小寫。)

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

確保 package.json 中的 Web 3 版本是

“web3”: “^1.0.0-beta.37”

引用自:https://ethereum.stackexchange.com/questions/52358