1

It's really weird that I got empty array after I tried to web3.eth.getAccounts().then(console.log);and I also got a warning which is ./node_modules/web3-eth-accounts/src/scrypt.js Critical dependency: the request of a dependency is an expression. In this project I first cmd create-react-app lottery_react and then all I changed in my lottery_react folder are modifying App.js and creating web3.js file. I can't find what's wrong in these file. Please help!

I've seen this but I didn't get the error that he/she gets. I only get a warning.

This is my App.js

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';

import web3 from './web3';

class App extends Component {
  render(){
    web3.eth.getAccounts().then(console.log);
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

export default App;

This is my web3.js file

import Web3 from 'web3';

const web3 = new Web3(window.web3.currentProvider);

export default web3;
  • Well what node are you connected to? What is the configuration of that node? – goodvibration Sep 16 at 6:06
  • @goodvibration Thanks for replying me. I'm using Rinkeby as my test networks. How can I see the configuration of my node? Please be more specific. – Jessie Sep 16 at 6:08
  • web3.eth.getAccounts() gives you a list of unlocked accounts on your node. It works well with Ganache (which by default sets up a node with 10 unlocked accounts). But why should it work on a public network like Rinkeby? – goodvibration Sep 16 at 8:13
  • @goodvibration If you are using provider from browser wallet you should get the selected account as return value. That is the expected behavior. – Sanjay S B Sep 16 at 8:23
0

I can connect with my private ethereum network using the following code:

web3.eth.getAccounts(function(err, accountList) {
        if(!err)
           console.log(accountList);
});

Hope you can do so.

  • This will still return empty array! I've tried. – Jessie Sep 16 at 7:18
  • Do you have keystores in the node which you are connecting to ? Is the node connected with web3? – Md Rayhanul Masud Saom Sep 16 at 7:37
  • Where can I connect keystores? How do I know if the node is connected with web3? All I do right now is to access into metamask accounts and then create my web provider with the web provider that metamask provided. – Jessie Sep 16 at 7:39
  • Please be more specific. – Jessie Sep 16 at 7:58
  • Have you added accounts in metamask? – Md Rayhanul Masud Saom Sep 16 at 8:20
0

In my MetaMask, I go to the setting and choose Connection and add site localhost. In the end, the return value from web3.eth.getAccounts().then(console.log); no longer return empty array. However, it still pops up warning webpackHotDevClient.js:120 ./node_modules/web3-eth-accounts/src/scrypt.js Critical dependency: the request of a dependency is an expression, I still can't fix it.

0

I was facing the same issue on an old project which was working fine till recent past. Metamask changes are the cause. You have to call the enable function to access the accounts now. The privacy option in the settings have been removed. The DApp has to initiate from its side. Add this piece of code in the console. You will be prompted for a permission. Accept and then try accessing the accounts. If it works add in the code (for you in the web3.js file above web3 = new Web3(window.web3.currentProvider);).

window.ethereum.enable();

For me the following is the behaviour in console for any website other than new tab:

//the injected web3 is 0.20.7
web3.eth.accounts;
//returns empty array

window.ethereum.enable();
//Page refreshes after you Accept

web3.eth.accounts;
//returns the first account

EDIT: This also works:

const web3 = new Web3(window['ethereum'] || window.web3.currentProvider);

This is from Metamask API reference page.

The page explains that window.web3.currentProvider is now legacy.

Your Answer

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

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