I'm following truffle tutorial to interact with a deployed contract and have a naive question. How do I run this block of multiline scripts in truffle(development)> without collapsing it into a single line?

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;
  return meta.sendCoin(account_two, 10, {from: account_one});
}).then(function(result) {
  // If this callback is called, the transaction was successfully processed.
  alert("Transaction successful!")
}).catch(function(e) {
  // There was an error! Handle it.
  • 1
    I haven't used truffle myself, but in Node.js, you could just paste this in (or type it by hand), and the newlines wouldn't be a problem. Does the truffle console not work the same way?
    – user19510
    Jan 17, 2018 at 0:14
  • Unfortunately, no. It throws SyntaxError: Unexpected end of input at MetaCoin.deployed().then(function(instance) {. Jan 17, 2018 at 0:27
  • Disappointing! I'm sure someone will come along with a helpful answer. :-) Good luck!
    – user19510
    Jan 17, 2018 at 0:31
  • Are you looking for a particular solution with a console or you just need a solution to run this code? Sorry, I just need to clarify. Jan 17, 2018 at 1:32
  • 1
    If this is your first truffle tutorial, let me suggest this one. Just because it will cover every topic in the development. As for tutorial that you are following you may need to complete the code to actually run it. Jan 17, 2018 at 2:14

2 Answers 2


You can simply require the file you want to run, just like how you would in a node application:

$ echo "console.log('hello')" > myfile.js
$ truffle console
truffle(development)> require('./myfile.js')

However, the deployed contract won't be available but truffle does offer the exec command which injects a web3 provider. You'd just have to create the contract instance.

Example myfile.js

const artifacts = require('./build/contracts/MyContract.json')
const contract = require('truffle-contract')
const MyContract = contract(artifacts);

.then(function(instance) {
.catch(function(error) {

Then run in console:

$ truffle console
truffle(development)> exec ./myfile.js
  • Your example works. But as soon as I use MetaCoin in myfile.js, it complains ReferenceError: MetaCoin is not defined. Jan 17, 2018 at 5:57
  • @sinoTrinity are you sure you compiled it and see the artifacts in build/contracts/MetaCoin.json? The paths are relative as well so make sure your file is in the correct directory.
    – rhlsthrm
    Jan 17, 2018 at 22:48
  • 1
    Works after I install truffle-contract. Jan 18, 2018 at 0:23
  • When running it with exec the script always gets stuck: ethereum.stackexchange.com/questions/58337/… maybe you can help me, thx?
    – Andi Giga
    Sep 8, 2018 at 10:43

Truffle-Contract is now deprecated: https://www.npmjs.com/package/truffle-contract

We should be using now: @truffle/contract https://www.npmjs.com/package/@truffle/contract

Also as of today this works on truffle console:

const CakeToken = artifacts.require("./CakeToken.sol")

module.exports = async function(callback) {
    const cakeToken = await CakeToken.deployed()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.