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
• 27,620 points
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,040 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
• 65,850 points
433 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,050 points
33 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
64 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,610 points
92 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,040 points
157 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.