I am developing an application in nodejs. I need to interact with my smart contract and also want to sign the transaction using metamask as I don't want to manage they keys. I copied the following code from the web3 docs but my id condition is never satisfied and "web3 is undefined" is printed in the conosle. On the other hand, if I enter web3 in my chromes console, web3 object with

currentProvider: MetamaskInpageProvider

is returned.

const express = require('express');
const Web3 = require('web3');

const router = express.Router();


    if (typeof web3 !== 'undefined') {
        console.log('Web3 found');
        web3 = new Web3(web3.currentProvider);
        web3.eth.defaultAccount = web3.eth.accounts[0];
    } else {
        console.error('web3 was undefined');


Please help me to sign transactions using metamask while developing in nodejs. P.S i tried to execute the above code with a timer to check race condition but still same result.

  • MetaMask will only work in the browser, it will not work in your backend.
    – Ismael
    Aug 4 '18 at 22:01

Your Answer

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

Browse other questions tagged or ask your own question.