1

I am trying to learn about web3.js I install

npm install ethereumjs-testrpc

and

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

then i using the following code

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);

error says web3.setProvider is not a function

2

I believe you want something like this. (Note the capitalization.)

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
| improve this answer | |
  • Thank you for your reply i found the solution. testrpc connection is stoped . i am not noticed that. Thank you – Sammu Sundar Jun 30 '18 at 10:04
1

Make sure your Web 3 version in package.json is

"web3": "^1.0.0-beta.37"

| improve this answer | |
1

Ok, I got the issue, According to the docs of Web3-docs-link (web3), setProvider() is used to change the provider of the web3, if you are setting the provider for the first time you have to use in this way:

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

// change provider
web3.setProvider('ws://localhost:8546');
// or
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.