Hotswap + Nesh. A more awesome Node.JS development experience.

Ever since working on some minor Clojure projects and getting some experience with the leiningen and LightTable, I’ve been expecting a little bit more from the programming environments I work in.

Anyway, as I’ve begun doing significantly more work using Node in the past few months, I decided to really dig into the available toolset. I’ve discovered a few things that have really made life much more pleasant.

Hotswap

Hot code reloads are pretty necessary for catching errors quickly and being able to interact with your code as you write it. The hotswap module helps with this nicely. It’s not perfect – changes in modules required by the modules you hotload aren’t recognized instantly, so unless you’re only working on a few modules you’ll probably have to restart your REPL session at some point, but it’s a big improvement over the standard REPL experience. All you have to do to get rolling is this:

require('hotswap');

and in your module…

module.change_code = 1;

Nesh

Nesh is a drop-in replacement for the standard Node REPL. It features code inspection, multiple language support (you can write CoffeeScript), convenience functions and a plugin system.

You can also start it as part of a node process, just like the standard REPL.

One thing that really annoys me about the Node REPL (that Nesh shares) which I haven’t figured out a good solution to yet is when (for example) a syntax error is thrown, the Node process bails and the REPL closes. I’m sure a solution is possible, I just haven’t tracked one down. A work-around, and otherwise generally useful practice, however, is to do some preprocessing to prepare my REPL environment. That way, I don’t need to load too much of what I was working on when I restart the REPL. This is possible with both the standard REPL (by modifying the REPL context) and with Nesh (by modifying the global scope). Here’s an example:

Say I want to load a specific REPL experience (for working in one part of my application vs another). I have a script which is included anytime I want to have a REPL:

var hotswap = require('hotswap'),
      nesh = require('nesh');

opts = {
    welcome: 'Welcome!',
    prompt: '> '
};

// Load user configuration
nesh.config.load();

module.exports = {
    start: function(callback){
        // Start the REPL
        nesh.start(opts, function (err) {
            if(callback) callback();
            if (err) {
                nesh.log.error(err);
            }
        });
    }
}

That bit loads hotswap and calls a callback when the REPL starts. The callback is what I use to inject what I want into the scope:

var repl     = require("./repl")
      fs         = require('fs');

repl.start(function(){
    config  = require('../config');
    lib1 = require(config.libdir+'/lib1');
    lib2 = require(config.libdir+'/lib2');
});

Voila! A very pleasant Node development experience – an interactive REPL with code hot swapping and configuration dirty work taken care of.

This entry was posted in JavaScript, NodeJS, Programming. Bookmark the permalink. Both comments and trackbacks are currently closed.