Truffle Console Script giving Error: Cannot find module

Hi,
I am trying to run the following script:

const messageBuild = require('./build/contracts/MyContract.json')
const MyContract = require('~/Truffle_programs/script_transfer_Eth_thr_Truffle/contracts/MyContract.sol');
const MyContractTester = require('~/Truffle_programs/script_transfer_Eth_thr_Truffle/contracts/MyContractTester.sol');
module.exports = (callback)=>{
var acc1 = accounts[1]
var acc2 = accounts[2]
const myC = MyContract.deployed()
const myCT =  MyContractTester.deployed()
web3.eth.sendTransaction({to:myC.address, from:acc2, value: web3.utils.toWei('11')})
balance2 =  web3.eth.getBalance(acc2)
web3.utils.fromWei(balance2, "ether")
mybal = web3.eth.getBalance(myC.address)
web3.utils.fromWei(mybal, "ether")
myC.sendTo(myCT.address, web3.utils.toWei('3',"ether"), {from:accounts[0]})
myCbal = web3.eth.getBalance(myC.address)
web3.utils.fromWei(myCbal, "ether")
myCTbal = web3.eth.getBalance(myCT.address)
web3.utils.fromWei(myCTbal, "ether")
}

but I am getting the error:

            truffle(development)> truffle exec ./myscript.js
            Using network 'development'.
            internal/modules/cjs/loader.js:638
            throw err;
            ^
        Error: Cannot find module '~/Truffle_programs/script_transfer_Eth_thr_Truffle/contracts/MyContract.sol'

I tried the “ls” command and found that the path is correct:

        @lc2530hz:~$ ls ~/Truffle_programs/script_transfer_Eth_thr_Truffle/contracts/
        Migrations.sol  MyContract.sol  MyContractTester.sol

Somebody please guide me.

Zulfi.

You may wish to ask the truffle team for support.

Hi, could you please share your contracts source code?

Hi,
Thanks a lot for your interest in solving my problem. I found that it is expecting js file. I would add the solidity contracts soon.

Zulfi.

1 Like

Hi,
MyContract SC:
pragma solidity ^0.5.8;

contract MyContract {

    address owner;

    constructor() public {
        owner = msg.sender;
    }

    function sendTo(address payable receiver, uint amount) public {
       receiver.transfer(amount);
    }

    function() external payable{
     }

}

and MyContractTester is:

pragma solidity ^0.5.8;
contract MyContractTester {
    //This contract will receive Ether sent by MyContract
    address owner;

    constructor() public {
       owner = msg.sender;
    }

    function() external payable {
       
    }
}

Please guide me how to test MyContract with the help of the script.

Zulfi.

There is a similar tutorial, maybe you can have a look:

1 Like