javascripthtmlethereummetamaskwallet-connect

Why is window.ethereum not working in the following code?


I am trying to create a dApp but the browser in visual studio code doesn't see the ethereum connection even though I've installed metamask on the running browser.

Here is my code (just to test the connection)

function testConnexion()
{    
    if(window.ethereum)
    {
        console.log("Active connection");
    }
    else if(!window.ethereum)
    {
        console.log("No connection")
    }
}

I've linked that function to a button in a basic html file, each time I click on the button, the log is "No connection" even when my metamask is manually connected to my website.

Is it impossible to connect to a wallet in a visual studio browser or is it something that I am doing wrong ?

Regards


Solution

  • Found it,

    In order for the browser to see the wallet connection you have to create your own server (I used npx http-server).

    Looks like visual studio code running browser doesn't allow the communication between the wallet extension and the browser but works fine on a server.