title: How to connect ethers.js to Metamask - await is only valid in async function error
body: <p>I'm trying to connect to Metamask using ethers.js.</p>
  <p>However, all the solution I found keep returning this error</p>
  <p>&quot;await is only valid in async function&quot;</p>
  <p>Was there a recent change in Javascript syntax that affects await?</p>
  <p><a href="https://stackoverflow.com/questions/60785630/how-to-connect-ethers-js-with-metamask">How to connect ethers.js with metamask?</a></p>
  <pre><code>const provider = new ethers.providers.Web3Provider(window.ethereum, &quot;any&quot;);
  // Prompt user for account connections
  await provider.send(&quot;eth_requestAccounts&quot;, []);
  const signer = provider.getSigner();
  console.log(&quot;Account:&quot;, await signer.getAddress());
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/68296594/how-to-connect-metamask-with-ethers-js-and-fetch-balance">how to connect metamask with ethers.js and fetch balance?</a></p>
  <pre><code>await window.ethereum.enable();
  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const contract = new ethers.Contract(smartContractAddress, abi, provider);
  balance = await contract.getBalance(&quot;0x7C76C63DB86bfB5437f7426F4C37b15098Bb81da&quot;);
  </code></pre>