I am working on a React app which uses a library that has Web3 as a dependency. I had previously been able to the get the current Metamask address with the following code:

 const injectedWeb3 = window.web3 || undefined;

 this.state = {
      web3: injectedWeb3

  getAccount() {
    const { web3 } = this.state;
    if (web3.eth.accounts[0]) return web3.eth.accounts[0];
    throw new Error('Your MetaMask is locked. Unlock it to continue.');

Then I updated that library to its latest version which changed it's Web3 dependency to Web3 1.0 . Now when I run the exact same code I get the following error:

Error: Invalid JSON RPC response: undefined 
TypeError: e is not a function[Learn More] 

Any thoughts as to what might be going on?

2 Answers 2


Found a workaround for this, you can use window.ethereum.enable() and attach it to the web3.eth.defaultAccount variable,

loadAccount : async () =>{
       window.ethereum.enable().then((account) =>{
           const defaultAccount = account[0]
           web3.eth.defaultAccount = defaultAccount


you can then access the Account ID / Address by using web3.eth.defaultAccount


Web3.js v1 has many differences from the previous version. Account request is now asynchronous:

web3.eth.getAccounts().then(it => {
            account = it[0];

If you need to wait for the operation to complete, use await


async function getAccount() {
    var account = 0;
    if (window.ethereum) { // for modern DApps browser
        window.web3 = new Web3(ethereum);
        try {
            await ethereum.enable();
        } catch (error) {
    }else if (web3) { // for old DApps browser
        window.web3 = new Web3(web3.currentProvider);
    } else {
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');

    if(window.web3!== 'undefined'){
            try {
                await window.web3.eth.getAccounts().then(it => {
                    account = it[0];
    return account;
  • I got the same error when I tried your code above. So far I am able to get 'web3.eth.getAccounts(console.log);' to display the account in the console. But when I even try 'web3.eth.getAccounts().then(console.log);' . It says the function is undefined and that 'callback is not a function'.
    – phoenixson
    Commented Mar 20, 2019 at 12:56
  • There is a 'null' before it outputs the array in the console when I use 'web3.eth.getAccounts(console.log);' . If that is of any significance?
    – phoenixson
    Commented Mar 20, 2019 at 13:01
  • What is the result of this code: web3.eth.getAccounts().then(it => { console.log(it[0]); });
    – Vasay
    Commented Mar 20, 2019 at 14:13
  • Another question - is injectedWeb3 defined? Web3js v1 is required to request metamask for permission to grant access rights to web3
    – Vasay
    Commented Mar 20, 2019 at 14:17
  • The result of the code snippet you put in the comments i: TypeError: web3.eth.getAccounts(...) is undefined[Learn More] TypeError: callback is not a function[Learn More]
    – phoenixson
    Commented Mar 20, 2019 at 16:36

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.