How to Connect Web3.js to MetaMask in 2021
The blockchain world always evolves so quickly, since it’s still a young and energetic field. Without programming with ethereum network for some time, I noticed lots of stuff has changed. This article is inspired by the article How to Connect Web3.js to MetaMask in 2020. It’s worth mentioning that, according to this article, in newer version MetaMask, we can simply use window.ethereum
as the provider
itself, without checking window.web3
for its currentProvider
.
However, the implementation mentioned in that article used the code window.ethereum.enable();
which still works until now, but there’s an warning:
MetaMask: ‘ethereum.enable()’ is deprecated and may be removed in the future. Please use the ‘eth_requestAccounts’ RPC method instead.
For more information, see: https://eips.ethereum.org/EIPS/eip-1102
So that I am not satisfied. Then I planned to write a new 2021 version article to introduce how to manage it without warning. (Even though there’s still 5 days remained in 2020 XD but I believe it doesn’t matter.)
We should use request API instead of ethereum.enable()
. (The ethereum.send()
also can work but has been deprecated similarly.) So I recommend using the following code to connect to MetaMask:
1 | const Web3 = require("web3"); |
But before connecting to MetaMask, typically we might want to also check if the browser supports the MetaMask, so we can use the following code to do that:
1 | if (typeof window.ethereum == "undefined") { |
Definitely you can adjust the code as your own dapp logic!
But just to notice that, this approach is to connect the MetaMask, not to test or listen whether it’s connected or not. If you want to listen on the connection changes, please see another of my article How to Listen on the Wallet State Changes in MetaMask in 2021.