[Ethereum] Unable to connect to metamask using web3

metamaskweb3js

this is what I am using

I have tried everything but just can't get the web3.currentProvider to work. I have metamask installed and working and somehow want to connect the website on the test network. Has there been some recent update which is not reflected in the documentation?

Best Answer

The problem here is likely that you are trying to run your page locally, which is explicitly restricted in MetaMask:

🌐 Http(s) - Web Server Required

Due to browser security restrictions, we can't communicate with dapps running on file://. Please use a local server for development.

You will need to publish your code to a local server to test MetaMask functionality.

In the situation where you do not need the MetaMask user context, you can use the HttpProvider fallback using infura.io:

https://github.com/shawntabrizi/ETH-Balance/blob/master/index.html#L13

window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));

This should allow you to do basic read operations on the Ethereum blockchain without needing to deploy to a local test server.

Related Topic