Andrew Gusty
Andrew Gusty

Reputation: 131

"@metamask/detect-provider: Unable to detect window.ethereum" error even though Metamask is installed and running

I am trying to run a basic program that detects if the user has window.ethereum installed. When I run my program I get "@metamask/detect-provider: Unable to detect window.ethereum", even though I have Metamask installed and running in my browser. Has anyone had similar problems? Thanks in advance.

Javascript:

import detectEthereumProvider from '@metamask/detect-provider';
 
const provider = await detectEthereumProvider();
 
if (provider) {
 
  console.log('Ethereum successfully detected!');
 
  })
} else {
 
  console.error('Please install MetaMask!', error);
}

Upvotes: 6

Views: 7675

Answers (2)

Andrew Gusty
Andrew Gusty

Reputation: 131

Turns out this was rather simple. Metamask only injects window.ethereum into visited websites that are running on an http server. I just had to open my website using a localhost server from Node.

Upvotes: 7

mrblue
mrblue

Reputation: 237

If you want to check if metamask is installed, you can do it this way:

if (window.ethereum && window.ethereum.isMetaMask) {
  // metamask is installed
}

Metamask is adding window.ethereum object to the browser. The code above is checking if there's window.ethereum object available in client browser and second condition, window.ethereum.isMetaMask is checking if the window.ethereum object is injected by metamask.

Upvotes: 2

Related Questions