I am following this tutorial here:
after running webpack
I try to view the page but it returns a blank page.
When I check the developers console, I get the following error:
Uncaught ReferenceError: web3 is not defined at new App (build.js:10665) at constructClassInstance (build.js:19093) at updateClassComponent (build.js:20577) at beginWork (build.js:20963) at performUnitOfWork (build.js:22962) at workLoop (build.js:23026) at HTMLUnknownElement.callCallback (build.js:13280) at Object.invokeGuardedCallbackDev (build.js:13319) at invokeGuardedCallback (build.js:13176) at renderRoot (build.js:23104) build.js:22485 The above error occurred in the component: in App
Consider adding an error boundary to your tree to customize error handling behavior. Visit https://fb.me/react-error-boundaries to learn more about error boundaries. logCapturedError @ build.js:22485 build.js:23732 Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development. See https://fb.me/react-crossorigin-error for more information. at Object.invokeGuardedCallbackDev (build.js:13326) at invokeGuardedCallback (build.js:13176) at renderRoot (build.js:23104) at performWorkOnRoot (build.js:23752) at performWork (build.js:23705) at requestWork (build.js:23616) at scheduleWorkImpl (build.js:23470) at scheduleWork (build.js:23427) at scheduleTopLevelUpdate (build.js:23931) at Object.updateContainer (build.js:23969)
This is strange as my index.js (the file from which I am building from ) has
import Web3 from 'web3'
I have even tried swapping for
Web3 = require('web3')
but to no avail.
These are the references to web3
in the js file:
import React from 'react'
import ReactDOM from 'react-dom'
import Web3 from 'web3'
import './../css/index.css'
class App extends React.Component {
/*
Constructor and set the initial state of the application
*/
constructor(props){
super(props)
this.state = {
lastWinner: 0,
numberOfBets: 0,
minimumBet: 0,
totalBet: 0,
maxAmountOfBets: 0,
}
/*
Checking to see if Web3 variable we imported is defined or not
*/
if(typeof web3 != 'undefined'){
console.log("Using web3 detected from external source like Metamask")
this.web3 = new Web3(web3.currentProvider)
}else{
this.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
}
What am I doing wrong?
Web3
(uppercase). They won't define anything calledweb3
(lowercase). I do see code in that blog post that saysweb3.eth.contract(...)
. This should probably readthis.web3.eth.contract(...)
instead. But I'm not sure if that's where you're encountering the error. You might need to simplify your code down to a minimal repro and then share it.again
). Will cut out the web 3 references and post. Thank youweb3
references that should actually bethis.web3
. (Those things probably work fine if you have MetaMask or another injector ofweb3
around, but without that, it's not going to be defined.)