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


This Content is from Stack Overflow. Question asked by Thomas L

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()
        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 ?



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.

This Question was asked in StackOverflow by Thomas L and Answered by Thomas L It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?