Interacting with app code in the node REPL

One of the pleasures of frameworks like Rails is being able to interact with models on the command line. Being very new to node.js, I often find myself pasting chunks of app code into the REPL to play with objects. It's dirty.

Is there a magic bullet that more experienced node developers use to get access to their app specific stuff from within the node prompt? Would a solution be to package up the whole app, or parts of the app, into modules to be require()d? I'm still living in one-big-ol'-file land, so pulling everything out is, while inevitable, a little daunting.

Thanks in advance for any helpful hints you can offer!

One-big-ol'-file land is actually a good place to be in for what you want to do. Nodejs can also require it's REPL in the code itself, which will save you copy and pasting.

Here is a simple example from one of my projects. Near the top of your file do something similar to this:

function _cb() {
  console.log(arguments)
}

var repl = require("repl");
var context = repl.start("$ ").context;
context.cb = _cb;

Now just add to the context throughout your code. The _cb is a dummy callback to play with function calls that require one (and see what they'll return).

Seems like the REPL API has changed quite a bit, this code works for me:

  var replServer = repl.start({
    prompt: "node > ",
    input: process.stdin,
    output: process.stdout,
    useGlobal: true
  });
  replServer.on('exit', function() {
    console.log("REPL DONE");
  });

You can also take a look at this answer http://stackoverflow.com/a/27536499/1936097. This code will automatically load a REPL if the file is run directly from node AND add all your declared methods and variables to the context automatically.