I have a very strange bug in the code. When loading a page with a code, the current account in metamask should be determined. but this only happens on one local domain. in other domaines accounts are empty. It is in chrome. In other browsers (firefox) it never finds accounts at all. Metamask is open to all sites.

let w3, myAddress, myContract;
async function connect() {
 try {
    if (window.web3 !== 'undefined') {
      console.log("Using Metamask's web3 provider");
      w3 = new Web3(window.web3.currentProvider);
let promise = await w3.eth.getAccounts()
       .then(accounts => {
        let Address = accounts[0]; // get address of account in Metamask
      return Address;
    myAddress =  await promise;
// is empty
    let wallet =  w3.eth.accounts.wallet;
    if (!myAddress) {
        alert('account not found'); }
   } catch (err) { console.error(err); }// err in empty

If your MetaMask is open, it means you don't need to get permission anymore. however new MetaMask can be found in window.ethereum not in window.web3. only legacy (outdated) MetaMask users still have window.web3.

Try this Code

import Web3 from 'web3';

let w3
if(typeof window !== 'undefined' && typeof window.ethereum !== 'undefined'){
  //getting Permission to access
  w3 = new Web3(window.ethereum);

}else if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
  w3 = new Web3(window.web3.currentProvider);
  // In legacy MetaMask acccounts are always exposed

} else {
  alert("No MetaMask detected, please install MetaMask first");
  • Thank you for replay. But I dont understand how to import module Web3 yet. – OneTwo May 7 '20 at 6:40
  • open your terminal on your current folder. then enter "npm install web3". – Masoud jt May 7 '20 at 6:42
  • visit web3.js docs for more installation helps. Here – Masoud jt May 7 '20 at 6:45
  • thank you. will read. – OneTwo May 7 '20 at 6:46
  • I use dist/web3.min.js in pages of virtual domain on local mashine – OneTwo May 7 '20 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.