I apologize preemptively for the very basic question. I have added and installed web3 using yarn in my react project. Inside my index.js file in /scr and based on the web3 docs I have tried both (not at the same time)
var Web3 = require("web3");
var web3 = new Web3();
and
import Web3 from 'web3';
var web3 = new Web3();
before adding
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("http://localhost:8545"));
}
web3.eth.getAccounts((error, accounts) => console.log(accounts[0]));
and end up with an error complaining "Cannot read property 0 of undefined" leading me to think the issue is with accounts[0].
Confusingly, when my require/import statements at the top of the page are deleted and the page is refreshed, the browser detects metamask and prints my address to the console. Additionally I get complaints that my web3 object is undefined but that is to be expected.
I have not explicitly run webpack or browserify because webpack is included in react projects. Is this something I need to do?
What is going on here and how can I get my React project to work with metamask?