0

I am trying to deploy my contracts with the specific account the user is using instead of hard coding it. But in this code, I get an error as such:

Error: Cannot find module 'web3'

the truffle-config.js file is as follows:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
var acc;
web3.eth.getCoinbase(function(err, account) {
      if (err === null) {
        acc=account;
      }
    })

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*", // Match any network id
      from: acc
    },
    develop: {
      port: 8545
    }
  }
};

How to overcome this error?

2
  • did you install web3 package?. If not then install it with npm install web3 May 15, 2020 at 12:04
  • That is not the right way for deployment
    – Majd TL
    Mar 27, 2021 at 15:56

2 Answers 2

1

Try using import instead of require. change this code:

const Web3 = require('web3');

to this code:

import Web3 from 'web3';
1
  • tried this but showed the following error: SyntaxError: Cannot use import statement outside a module
    – Shras299
    May 15, 2020 at 6:08
0

Have you tried:

npm install web3

It installs web3 to your local dependencies. But I'm wondering what you are trying to achieve with that Web3 call in the first place?

1
  • In my application, the ethers shud be transferred from the user account only. But in truffle, the default will be account[0] only. I don't want that so trying to set up the from account using web3
    – Shras299
    May 15, 2020 at 6:05

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.