Web3js – Web3js Not Connecting to TestRPC in Localhost

javascriptnodestestrpcweb3js

I am simply trying to run web3js in a node console and connect it to a testrpc running in localhost.

I run my testrpc like so:

> ganache-cli
Ganache CLI v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x131f68e69281532b0ced46cb5da5a5b8cd676b81
(1) 0xb7f672193eb7653912fdbe623699aa21c5d6a9c7
(2) 0x4b6021ac75768c1872ec10d4da31046f37e64d31
(3) 0x0d1f5030fbca0b6ed939093882e3fd788ddb91c6
(4) 0xb4d33e34e1743b07b2599bc532bcf9583f7019b2                                                                                                                                                                      [16/3254](5) 0x160343f0e731075149608a48aed614328127336f
(6) 0xf0c9c25fa5d496263176c07ea0d73172f8566d4f
(7) 0xffcc8d7d07334fc0af7d027378a20289b708e5be
(8) 0x453fca917e47ed8786a05613c3f3de89d57b3c8f
(9) 0x377a23e76fa9bc0574c30a64df4ca9b186c3976b

Private Keys
==================
(0) 8e79cd0667cf265d7241087af29788a7bbe6e2608b436cfe9f775153b5516f31
(1) c90108c817b0648f7fb7795f5b21c45bd3ba016aefa8dafc0d74d39ff68960ef
(2) bf7b186071640f8e986f2e78edf41643e3e1f8aedbfc51fb9410ad9f2a5d502b                                                                                                                                                 [5/3254](3) f40c6a05337b137cc084991a2743d67bf24d27206ffaba8432864e10edb66860
(4) de0008cef56816baea1f0363e6c9af3a1e69821380d72879fc0e2270cceb2044
(5) 0b73cbb9226ec939084831cb749f198dce80b39057b66c121cbdfc6627994f69
(6) a37f478448aabb7ff895d53165bb74b27c9913a2d7130b9e0cd587915615f08d
(7) 40790b3db911d0e4812a164f1778e2d74c3f79589c4e84ee4a1a6b87e25ad69a
(8) 42536e4ddc80bfb0104a265f94c50e65d2484007a9d56200a409de27d86358f0
(9) 572432cf3dc96494900175b66428a40f107de0a9ff832cce9e8aad2385ae22be

HD Wallet
==================
Mnemonic:      between trust fossil over stand make suffer behind raccoon long engage height
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

This looks all good to me, I then open a tmux session and run the following lines in a simple node cli:

// Load Libraries
  var solc = require("solc");
  var fs = require("fs");
  var Web3 = require("web3");

// Connect Web3 Instance
  var web3 = new Web3(new Web3.providers.HttpProvider(`http://localhost:8545`));

// Global Account Accessors
  var acct1 = web3.eth.accounts[0];

> acct1
undefined

As you can see the value acct1 is empty, when I go back to my tmux session running testrpc, I cannont see the eth_account command which should have been executed.

If I run netstat -tuplen I can indeed see that testrpc is running on port 8545:

tcp6       0      0 :::8545                 :::*                    LISTEN      1000       3431613     50196/node
udp        0      0 0.0.0.0:68              0.0.0.0:*                           0          15715

Best Answer

It looks like there were many changes made in web3 so to now access the accounts you need to use the following:

web3.eth.personal.getAccounts().then(res => console.log(res)) to list all accounts ..... or web3.eth.personal.getAccounts().then(res => console.log(res[0])) to display account[0] etc