Hi,
I used web3.js in nodejs, but it always return undefined。I tried a few of ways, but it doesn't work. can you help me?
here is the code
var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var account = web3.eth.accounts[0];
console.log(account);
here is the result:
➜ web3test node test.js
undefined
geth is running:
tfd 20313 5.2 0.7 2158548 117684 pts/3 SNl 16:24 1:08 geth --datadir /home/tfd/workspace/pchain/ --rpc --rpcapi db,admin,debug,eth,miner,net,personal,web3,txpool --rpcaddr localhost --rpcport 8545 --port 30301 --networkid 1108
node version:
➜ web3test node -v
v8.11.2
Ubuntu 17.10
I also tried var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); but it still doesn't work, even web3.version.api returned undefined.
Have you started some Ethereum client (e.g., Geth, Parity, Ganache, TestRpc) listening at http://localhost:8545?
You should do that, and when you do, you can specify the number of available accounts and the characteristics of each account.
For example (pardon my Windows-ness):
start cmd /c "title ganache-cli & ganache-cli" --port=8545 --gasLimit=0xfffffffffff ^
--account=0x0000000000000000000000000000000000000000000000000000000000000001,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000002,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000003,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000004,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000005,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000006,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000007,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000008,1000000000000000000000000000000000000000
Thank you very much!I started geth,and I can get the accounts in geth console with ‘eth.accounts’。 But I can't get the accounts with web3.js。
I start geth with this command:
geth --datadir /home/tfd/workspace/pchain/ --rpc --rpcapi db,admin,debug,eth,miner,net,personal,web3,txpool --rpcaddr localhost --rpcport 8545 --port 30301 --networkid 1108
At 2018-06-20 19:03:32, "barakman" notifications@github.com wrote:
Have you started some Ethereum client (e.g., Geth, Parity, Ganache, TestRpc) listening at http://localhost:8545?
You should do that, and when you do, you can specify the number of available accounts and the characteristics of each account.
For example (pardon my Windows-ness):
start cmd /c "title ganache-cli & ganache-cli" --port=8545 --gasLimit=0xfffffffffff ^
--account=0x0000000000000000000000000000000000000000000000000000000000000001,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000002,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000003,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000004,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000005,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000006,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000007,1000000000000000000000000000000000000000 ^
--account=0x0000000000000000000000000000000000000000000000000000000000000008,1000000000000000000000000000000000000000
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
What version of web3.js do you have installed?
(Run npm ls web3 from the location of your package.json file).
@barakman Thank you !
Here is the result of npm ls web3:
├── UNMET PEER DEPENDENCY [email protected] - 4.x
├── UNMET PEER DEPENDENCY popper.js@^1.14.3
└── [email protected]
npm ERR! peer dep missing: popper.js@^1.14.3, required by [email protected]
npm ERR! peer dep missing: [email protected] - 4.x, required by [email protected]
Please use web3.eth.getAccounts() instead.
AFAIK, web3.eth.accounts[0] used to work in older versions of web3 (0.20.0)
Most helpful comment
Please use web3.eth.getAccounts() instead.
AFAIK,
web3.eth.accounts[0]used to work in older versions of web3 (0.20.0)