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.
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?

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'.
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.
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

truffle test on other hand seem to be working fine but does not display the status of tests
Most helpful comment
@mathcrypto maybe this helps:
https://github.com/trufflesuite/truffle/issues/676
Is your script wrapped into
module.exports