Web is not injected by metamask

0 votes

'web3 is not defined' even though the metamask is installed and logged in with an address.This works in online editors like codepen and JSFiddle.Tried window.web3, and the result was same.But, window.Web3 is returning a function.What is the difference and Where am I wrong?

  window.addEventListener("load", function() {
    if (typeof web3 !== "undefined") {
      web3 = new Web3(web3.currentProvider);
      console.log("Pulling metamas as current provider");
      web3.eth.getAccounts(function(error, accounts) {
        if (!error) {
           getAccountBalance(accounts[0])
        } else {
           console.error(error);
        }
      });
    }else {
      console.log("No metamask ? Pulling kovan testnet as current provider");
      web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io"));
    }
});
Oct 3, 2018 in Blockchain by digger
• 26,740 points
856 views

1 answer to this question.

0 votes

You need to do import the Web3 library, so use the following code:

const Web3 = require('web3')

Then you can use web3 = new Web3(...) normally.

answered Oct 3, 2018 by slayer
• 29,360 points

Related Questions In Blockchain

+1 vote
2 answers

Integrating web3 from Metamask: "ReferenceError: window is not defined" error

You can't use MetaMask on server-side because ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,230 points
6,797 views
+1 vote
1 answer

How does a miner get to know that a transaction is verified by all the nodes?

Contrary to the popular belief, it is ...READ MORE

answered Mar 27, 2018 in Blockchain by Johnathon
• 9,090 points
2,633 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,787 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,311 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,091 views
0 votes
1 answer

Truffle tutorials "Error:recipient address is not a contract address"

It appears like you have already migrated ...READ MORE

answered Aug 8, 2018 in Blockchain by slayer
• 29,360 points
937 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP