Need more detailed description how to install it on OSX with NPM because I get "undefined provider" error in the best case and various red errors in chrome console in the worst case.
Your suggested code doesn't see Metamask.
sudo apt-get install nodejs-legacy - this command works in linux only and it is unclear what to do in OSX.
In the same time the library somehow works with networks like ganache and infura.
And in the same time the library works if it is installed via Meteor
Example of code:
<html>
<head></head>
<body>
<script src="web3.js"></script>
<script>
window.addEventListener('load', function() {
var Web3 = require('web3');
var web3 = new Web3();
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
web3js = new Web3(web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!')
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
web3js = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
web3js.version.getNetwork((err, netId) => {
console.log(err);
switch (netId) {
case "1":
console.log('This is mainnet')
break
case "2":
console.log('This is the deprecated Morden test network.')
break
case "3":
console.log('This is the ropsten test network.')
break
case "4":
console.log('This is the Rinkeby test network.')
break
case "42":
console.log('This is the Kovan test network.')
break
default:
console.log('This is an unknown network.')
}
})
})
</script>
</body>
</html>
Console output:
Error: Provider not set or invalid
at Object.InvalidProvider (web3.js:3134)
at RequestManager.sendAsync (web3.js:6365)
at Object.get [as getNetwork] (web3.js:6259)
at index.html:21
Need more detailed description how to install it on OSX with NPM because I get "undefined provider" error in the best case and various red errors in chrome console in the worst case. Your suggested code doesn't see Metamask.
sudo apt-get install nodejs-legacy
- this command works in linux only and it is unclear what to do in OSX. In the same time the library somehow works with networks like ganache and infura. And in the same time the library works if it is installed via MeteorExample of code:
Console output: