truffle exec does not handle require statements properly

Created on 29 Aug 2016  ยท  19Comments  ยท  Source: trufflesuite/truffle

Summary: truffle exec <example_script> does not properly handle require statements for local node modules in example_script.

Details: truffle exec uses the function at https://github.com/ConsenSys/truffle/blob/master/lib/require.js#L38 to handle require statements. However, if a local module is required, the line at https://github.com/ConsenSys/truffle/blob/master/lib/require.js#L53 assumes that the node_modules directory is present in the same directory as the script being executed, rather than searching upward for the root of the node package before looking for node_modules.

bug

Most helpful comment

@mathcrypto maybe this helps:
https://github.com/trufflesuite/truffle/issues/676
Is your script wrapped into module.exports

All 19 comments

Thanks @mrbodoia! Verified bug. We should probably use find-up in the require function to solve it.

But I have my script in the project root where also node_modules resides and it still doesn't work. With node <script> it works

@rolandkofler Since your issue might be different than @mrbodoia, I need some clarification: are you require'ing a local module (require("./path/to/some/module")) or an installed module (require("mysql"))?

@tcoulter your intuition is right, I'm a newbe with node.
node install eth-lightwallet
but neither
var lightwallet = require('./node_modules/eth-lightwallet');
nor
var lightwallet = require('eth-lightwallet');
does the trick.

Very interesting. Can you show me the layout of the script you're trying to run and the directories around it? i.e.,

/myproject
  /contracts
  /test
  /node_modules
  /somescript.js <-- my script

Also, would you mind showing me the script?

.
โ”œโ”€โ”€ app
โ”‚ย ย  โ”œโ”€โ”€ images
โ”‚ย ย  โ”œโ”€โ”€ index.html
โ”‚ย ย  โ”œโ”€โ”€ javascripts
โ”‚ย ย  โ””โ”€โ”€ stylesheets
โ”œโ”€โ”€ build
โ”‚ย ย  โ””โ”€โ”€ config.gypi
โ”œโ”€โ”€ contracts
โ”‚ย ย  โ”œโ”€โ”€ GalaxiasV1.sol
โ”‚ย ย  โ””โ”€โ”€ Migrations.sol
โ”œโ”€โ”€ createaccountspike.js <------ my script
โ”œโ”€โ”€ migrations
โ”‚ย ย  โ”œโ”€โ”€ 1_initial_migration.js
โ”‚ย ย  โ””โ”€โ”€ 2_deploy_contracts.js
โ”œโ”€โ”€ node_modules
โ”‚ย ย  โ”œโ”€โ”€ bignumber.js
โ”‚ย ย  โ”œโ”€โ”€ bindings
โ”‚ย ย  โ”œโ”€โ”€ bip66
โ”‚ย ย  โ”œโ”€โ”€ bitcore-lib
โ”‚ย ย  โ”œโ”€โ”€ bitcore-mnemonic
โ”‚ย ย  โ”œโ”€โ”€ bn.js
โ”‚ย ย  โ”œโ”€โ”€ brorand
โ”‚ย ย  โ”œโ”€โ”€ browserify-aes
โ”‚ย ย  โ”œโ”€โ”€ browserify-cryptojs
โ”‚ย ย  โ”œโ”€โ”€ browserify-sha3
โ”‚ย ย  โ”œโ”€โ”€ buffer-xor
โ”‚ย ย  โ”œโ”€โ”€ cipher-base
โ”‚ย ย  โ”œโ”€โ”€ create-hash
โ”‚ย ย  โ”œโ”€โ”€ create-hmac
โ”‚ย ย  โ”œโ”€โ”€ crypto-js
โ”‚ย ย  โ”œโ”€โ”€ drbg.js
โ”‚ย ย  โ”œโ”€โ”€ elliptic
โ”‚ย ย  โ”œโ”€โ”€ ethereum-common
โ”‚ย ย  โ”œโ”€โ”€ ethereumjs-tx
โ”‚ย ย  โ”œโ”€โ”€ ethereumjs-util
โ”‚ย ย  โ”œโ”€โ”€ eth-lightwallet
โ”‚ย ย  โ”œโ”€โ”€ evp_bytestokey
โ”‚ย ย  โ”œโ”€โ”€ hash.js
โ”‚ย ย  โ”œโ”€โ”€ inherits
โ”‚ย ย  โ”œโ”€โ”€ js-sha3
โ”‚ย ย  โ”œโ”€โ”€ jszip
โ”‚ย ย  โ”œโ”€โ”€ keccakjs
โ”‚ย ย  โ”œโ”€โ”€ localstorejs
โ”‚ย ย  โ”œโ”€โ”€ nan
โ”‚ย ย  โ”œโ”€โ”€ node-safe-filesaver
โ”‚ย ย  โ”œโ”€โ”€ pako
โ”‚ย ย  โ”œโ”€โ”€ ripemd160
โ”‚ย ย  โ”œโ”€โ”€ rlp
โ”‚ย ย  โ”œโ”€โ”€ scrypt-async
โ”‚ย ย  โ”œโ”€โ”€ secp256k1
โ”‚ย ย  โ”œโ”€โ”€ sha3
โ”‚ย ย  โ”œโ”€โ”€ sha.js
โ”‚ย ย  โ”œโ”€โ”€ tweetnacl
โ”‚ย ย  โ”œโ”€โ”€ underscore
โ”‚ย ย  โ”œโ”€โ”€ unorm
โ”‚ย ย  โ”œโ”€โ”€ utf8
โ”‚ย ย  โ”œโ”€โ”€ web3
โ”‚ย ย  โ””โ”€โ”€ xmlhttprequest
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ test
โ”‚ย ย  โ””โ”€โ”€ galaxiasv1.js
โ””โ”€โ”€ truffle.js
var lightwallet = require('./node_modules/eth-lightwallet');

// generate a new BIP32 12-word seed
var secretSeed = lightwallet.keystore.generateRandomSeed();

// the seed is stored encrypted by a user-defined password
var password = 'Enter password for encryption';
lightwallet.keystore.deriveKeyFromPassword(password, function (err, pwDerivedKey) {

var ks = new lightwallet.keystore(secretSeed, pwDerivedKey);

// generate five new address/private key pairs
// the corresponding private keys are also encrypted
ks.generateNewAddress(pwDerivedKey, 5);
var addr = ks.getAddresses();
console.log(addr);

// Create a custom passwordProvider to prompt the user to enter their
// password whenever the hooked web3 provider issues a sendTransaction
// call.
ks.passwordProvider = function (callback) {
  callback(null, password);
};

// Now set ks as transaction_signer in the hooked web3 provider
// and you can start using web3 using the keys/addresses in ks!
});

@rolandkofler Are you getting an error message, or does the require just not seem to be working?

usr/local/lib/node_modules/truffle/lib/require.js:128
        fn(done);
        ^

TypeError: fn is not a function
    at /usr/local/lib/node_modules/truffle/lib/require.js:128:9
    at /usr/local/lib/node_modules/truffle/lib/require.js:82:7
    at tryToString (fs.js:455:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:12)

Ah, your issue is different than @mrbodoia. Due to technical reasons, truffle exec in Truffle 2.0 requires your script to output a module that's passed a callback function. Details here: http://truffle.readthedocs.io/en/latest/getting_started/scripts/

Is this also true of truffle migrate? I think I'm running into the same issue there:

2_deploy_contracts.js:

const Web3 = require('web3')

Output:

$ truffle migrate --reset
Running migration: 1_initial_migration.js
  Deploying Migrations...
  Migrations: 0xdbc4c7d5b2dbfc1c36320ba149c1df076e87b7f8
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js

/Users/raine/projects/myproject/node_modules/truffle/lib/require.js:59
              throw e;
              ^
Error: Cannot find module 'web3'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at context.require (/Users/raine/projects/myproject/node_modules/truffle/lib/require.js:55:22)
    at /Users/raine/projects/myproject/migrations/2_deploy_contracts.js:4:14

Truffle v3.0.0

This is fixed by Truffle 3.1.10 (beta). Will go out as an official release within the next day or so: https://github.com/trufflesuite/truffle/releases/tag/3.1.10

Thanks for the reports!

i am trying to load a script and i get this error
/Users/amira/.nvm/versions/node/v6.2.2/lib/node_modules/truffle/node_modules/truffle-require/index.js:117
fn(done);
^

TypeError: fn is not a function

i used thez command truffle exec script.js because i put it in the rootfile myproject

@mathcrypto maybe this helps:
https://github.com/trufflesuite/truffle/issues/676
Is your script wrapped into module.exports

So I seem to be having this problem again, even though I a using Truffle v5.0.13, is it possible this issue has returned?
Screen Shot 2019-05-09 at 10 41 09 AM

Yes I ran into this problem with a more recent truffle version(truffle 5.0.44) though this was fixed with the kind advice of @360disrupt and @MareoRaft.
please note that in order to run truffle exec <path/To/File> said file must have module.exports equal to one function that accepts one callback parameter, the code in that function is what will be executed upon truffle exec <path/To/File> It should look something like this:
module.exports = function(callback){/* To-Do */};
please visit the link posted by @MareoRaft for more detail

Getting following error ๐Ÿ’ฏ

D:\Blockchain\social-network>truffle test D:/Blockchain/social-network/tests/socialNetwork.js
You can improve web3's peformance when running Node.js versions older than 10.5.0 by installing the (deprecated) scrypt package in your project
You can improve web3's peformance when running Node.js versions older than 10.5.0 by installing the (deprecated) scrypt package in your project
Using network 'development'.

Compiling your contracts...

Everything is up to date, there is nothing to compile.

TypeError: fn is not a function
at Object.exports.use (D:\Blockchain\social-networknode_modules\chai\lib\chai.js:39:5)
at Object. (D:/Blockchain/social-network/tests/socialNetwork.js:4:6)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at loader (D:\Blockchain\social-networknode_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\Blockchain\social-networknode_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at C:\Users\rohit.mittal\AppData\Roaming\npmnode_modules\trufflenode_modules\mocha\lib\mocha.js:250:27
at Array.forEach ()
at Mocha.loadFiles (C:\Users\rohit.mittal\AppData\Roaming\npmnode_modules\trufflenode_modules\mocha\lib\mocha.js:247:14)
at Mocha.run (C:\Users\rohit.mittal\AppData\Roaming\npmnode_modules\trufflenode_modules\mocha\lib\mocha.js:576:10)
at resolve (C:\Users\rohit.mittal\AppData\Roaming\npmnode_modules\trufflebuild\webpack:\packages\core\lib\test.js:137:1)
at new Promise (D:\Blockchain\social-networknode_modules\core-js\modules\es6.promise.js:177:7)
at Object.run (C:\Users\rohit.mittal\AppData\Roaming\npmnode_modules\trufflebuild\webpack:\packages\core\lib\test.js:136:1)
at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.1.1 (core: 5.1.1)
Node v10.2.1
image

truffle test on other hand seem to be working fine but does not display the status of tests

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arunmitteam picture arunmitteam  ยท  3Comments

oed picture oed  ยท  3Comments

rstormsf picture rstormsf  ยท  3Comments

bmmpxf picture bmmpxf  ยท  3Comments

maximilianh picture maximilianh  ยท  3Comments