If you are encountering the errors below while trying to execute your Truffle external script, please continue reading.

Errors Encountered:

  • TypeError: Cannot read property 'apply' of undefined
  • exception at require.js:128:1
  • TypeError: fn is not a function

For some reason, it was quite difficult to find a solution on how to correctly run an external Truffle script. After some time, I finally figured it out and am sharing it with the world:

// This is an example of a correctly structured external Truffle script.
// It addresses common issues like 'apply' of undefined or 'fn is not a function'
// by ensuring the script exports an async function that accepts a callback.

module.exports = async function (callback) {
  try {
    // Example: Interact with a deployed contract
    const MyContract = artifacts.require("MyContract"); // Replace with your contract name
    const instance = await MyContract.deployed();
    console.log("MyContract instance address:", instance.address);

    // Perform operations with your contract instance
    // Example: const value = await instance.getValue();
    // console.log("Contract value:", value.toString());

    // It's crucial to call the callback function to signal completion
    // when your script has finished executing successfully.
    callback();
  } catch (error) {
    // If any error occurs, log it and pass it to the callback
    // This allows Truffle to report the error appropriately.
    console.error("Error during script execution:", error);
    callback(error);
  }
};

// To run this script, save it as, for example, 'my_script.js' in your 'scripts/' directory
// Then execute it using: `truffle exec scripts/my_script.js`