2

I need to create server in nodejs that will create transaction to blockchain and i have trouble to connect my nodejs app with ganache. Look at my code:

const Web3 = require('web3');
var web3 = new Web3.providers.HttpProvider('http://localhost:7545'); // ganache address
console.log(web3.eth.accounts); // should print 10 accounts but its error like eth is undefined

In my dependencies

"web3": "^0.20.2"

Using this code on client-side works, what am i missing?

8
  • 1
    Is ganache running on the server? Commented Aug 1, 2018 at 14:15
  • server is my laptop so yes
    – Pawelo
    Commented Aug 1, 2018 at 14:33
  • Probably won't help, but try http://127.0.0.1:7545 Commented Aug 1, 2018 at 14:35
  • nothing changed, im wonder if my web3 version is ok?
    – Pawelo
    Commented Aug 1, 2018 at 14:39
  • me to having the same problem please help![enter image description here](i.sstatic.net/86ACs.png)
    – vb7txxx31
    Commented Oct 13, 2018 at 17:34

1 Answer 1

6

This line is wrong:

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

You're instantiating a provider instead of instantiating Web3. It should be this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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