4

When i made web3 instance and run index.html i lose ethereum and window.ethereum when i call it from console i got ethereum is not defined,i have installed metamask and use ropsten network.Why its happend,but when i go on site like cryptokitties it works perfect.

if (typeof web3 !== 'undefined') {
 web3 = new Web3(web3.currentProvider);
} else {
 // set the provider you want from Web3.providers
 web3 = new Web3(new 
Web3.providers.HttpProvider("https://ropsten.infura.io/v3/148bee2b5da148a7b77a83f7504d00e7"));
}

async function asyncCall() {
  ethereum.enable()  // =========== RETURN UNDEFINED
  console.log('calling');
  const result = await web3.eth.getAccounts();
  console.log(result);
  // expected output: 'resolved'
}
asyncCall()

2 Answers 2

6

I was running into this same issue.
Apparently nobody wants to make the solution obvious, but I finally found this issue, which brought me to this link:

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

So setting up an actual server locally fixes the issue. I just verified that this works after installing http-server.

I installed it using npm install http-server -g (assuming node is installed).
Then ran it using http-server "C:\directory_of_my_html_file" -a 127.0.0.1 and was then able to successfully access window.ethereum (browsing to http://localhost:8080/index.html).

1
  • 1
    Thanks.Thats true,i solve it befor 2 weeks,but thanks Commented Apr 18, 2020 at 20:36
0

An easier way that worked for me is to install Live server extension for VSCode (VsCode users only)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.