Jon
Jon

Reputation: 168

React error: web3 is not defined no-undef

I installed web3 in a folder of react-app:

npm install --save ethereum/web3.js

but I get a react error:

web3 is not defined no-undef

import Web3 from 'web3'

export const startMetaMask = () => {

    if (window.ethereum) {
        web3 = new Web3(window.ethereum);
        try {
            window.ethereum.enable().then(function() {
                
            });
        } catch (e) {}
    }
    
    else if (window.web3) {
        web3 = new Web3(web3.currentProvider);
    }
    
    else {
        alert('You have to install MetaMask !');
    }
}

Upvotes: 0

Views: 4265

Answers (2)

Pacxiu
Pacxiu

Reputation: 33

If you installed your web3 like that:

npm install --save ethereum/web3.js

please check your package.json file to see how this library is viewed, probably it will be ethereum/web3. So your import should look more like that:

import Web3 from 'ethereum/web3'

If you wanted to install web3 package from npm, you can just do it with:

npm install --save web3

and import like you did. According to https://www.npmjs.com/package/web3

Upvotes: 1

Aziz.G
Aziz.G

Reputation: 3721

You should import package like this as mention in the Documentation

cause it's Named Export not Default export

import {Web3} from 'web3';

Upvotes: 0

Related Questions