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,600 points
54 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,240 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
• 68,480 points
735 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 26, 2018 in Blockchain by Johnathon
• 9,090 points
103 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,810 points
188 views
0 votes
1 answer
+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,620 points
182 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,240 points
226 views