0

I'm injecting web3 with MetaMask, and everything works, but when I make a production build, I notice that it injects web3 alright, but when I try to call a web3 function, it throws `Please provide an valid Web3 provider'; specifically the error is being thrown when I try to make a contact instance.

Here's my code

if(typeof web3 !== 'undefined') {
  console.log("Using web3 detected from external source like Metamask");
  web3 = new Web3(web3.currentProvider);
} else {
  window.alert("No web3 detected. This site is a Web3 Dapp - to proceed you will need to get Metamask https://metamask.io/");
}

...

this.contract = new web3.eth.Contract(abi, address)

When I run the development build, everything works fine. But when I make a production build using npm react-scripts build and run it, then I get an error.

Edit: Here is the error logged in the console enter image description here

  • Since last year Metamask recommends a different way to inject web3 in your app metamask.github.io/metamask-docs/API_Reference/… – Ismael Feb 8 at 1:09
  • provide error log if possible. – Mahesh Rajput Feb 8 at 5:36
  • It seems like my problem was with how react-scripts was creating the production build or with the project dependencies. I changed some things around and it's working now. – JarethRader Feb 9 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.