0

I am using Web3 v1.2.1 with nodejs v10 and following this link. I am having issue connecting with metamask,below is my code snippet:

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

The error says TypeError: Cannot read property 'currentProvider' of undefined

I want to access the address from metamask, can someone help me with it

2

Metamask is a browser extension, so you can only use it from the browser.

You can do this in the following way:

Create a file web3-require.js:

Web3=require('web3');

Install web3 and browserify

npm install web3
npm install browserify

Create a browserified version of web3:

browserify web3-require.js -o web3-browserify.js 

Create test.html:

Test metamask
<script src="./web3-browserify.js"></script>
<script type="text/javascript">
    web3 = new Web3(Web3.givenProvider);    
    ethereum.enable().then ( (x) => {
        alert(`Found address ${x[0]} with web3.js version ${web3.version}`); 
    } );
</script>   

Run this from a webserver (otherwise metamask won't insert "ethereum")

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.