I am a new!
code:
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
web3.isConnected()
why the error is web3.isConnected is not a function
?
thx!
Your problem is caused by the Web3 version your are currently using. I guess it is 1.0.0-beta.18
In package.json, you must have
"dependencies": {
"web3": "^1.0.0-beta.18"
}
Solution 1: Downgrade to a stable version
I recommend to downgrade to the latest stable version 0.19.0
i Edit package.json
ii Change the version of web3
"dependencies": {
"web3": "0.19.0"
}
Solution 2: Keep using the beta
First of all, the documentation is here.
I think (not 100% sure), this code should do the job to check if the client is connected or not to the node:
var Web3 = require('web3');
var provider = new Web3.providers.HttpProvider('http://localhost:8545');
var web3 = new Web3(provider);
console.log("isConnected=");
web3.eth.net.isListening().then(console.log);
You might need to install web3-net: npm install --save web3-net
You are using pre-1.0 version of web3 from a place where the provider object does not have the method isConnected()
defined.
From the picture above, I've guessing this is ganache-cli or truffle develop
, formerly testrpc. I guess these clients have a custom provider because both HttpProvider
and IpcProvider
do have these functions defined.
Apparently this method has been deprecated/removed from web3.js in 1.0.
Pre 1.0 docs: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3isconnected
Missing from 1.0-beta docs: http://web3js.readthedocs.io/en/1.0/web3.html