node package manager


Utilities for async shell scripting in node.js



Helper functions for running shell commands from node.js.


Obviously writing shell scripts is best done in shell. Except when you need some functionality that shell doesn't have, like parsing JSON, or using one of the gazillion modules available for node.


An dumbified version of a script that creates a tarfile for a project:

#!/usr/bin/env node
var fs = require('fs'),
    async = require('async'),
    sh = require('./shutil');

var vars = {
  REPO: "",
  VERSION: null,
  CURDIR: process.cwd(),
  TMPDIR: sh.tmpname('myproject'),

process.on('SIGINT', function() {}); // Just pass SIGINT down.

async.waterfall(['mkdir {TMPDIR}', vars),'{TMPDIR}', vars),'git clone --recursive {REPO} .', vars),'git describe --tags'),
  sh.assign('VERSION', vars),
  sh.sync(function() {
    var package = JSON.parse(fs.readFileSync('package.json'));
    return package.engines.node.match(/\s*[=v]?\s*(\d+\.\d+\.\d+)/)[1];
  sh.assign('NODE_VERSION', vars),'echo {NODE_VERSION} >NODE_VERSION', vars),'find . -name .git\\* -print0 | xargs -0 rm -rf'),'tar czf {CURDIR}/myproject-{VERSION}.tar.gz .', vars)],

  function(err) {
    async.series(['rm -rf {TMPDIR}', vars)], function() {
      if (err) {
      } else {


shutil provides a few functions that, when called, generate function which run a command and then call a callback with the result of that command. For example:

var f ='mkdir test');

f(function(err, stdout, stderr) {
  if (err) {
  } else {
    console.log("Result: " + stdout);

sh.sync() can be used to stick in synchronous JavaScript code which is run inside try/catch.

Let's say we want to use the result of one function in another. This won't work:

var value = false;
async.waterfall(['rm test'),
  sh.sync(function() { value = true; }),'echo value is ' + value),

It will always print false -- we need to evaluate value lazily. This is why all the functions support simple templating:'echo {a} is {b}', { a : 'templating', b : 'awesome' });

There's no escape characters, so if you need braces, stick them in vars, like:'echo left brace: {L}, right one: {R}', { L : '{', R : '}' });

Functions[, vars])

Runs the given shell command.[, vars])

Changes the current working directory to dir.


Runs the given function synchronously, and call a callback afterwards. Only useful if your async library doesn't catch exceptions itself.

shutil.assign(keyname, vars)

Grab the first parameter out passed to it (stdout, if it were called by one of the other functions), trim it, and store it under keyname in vars.

Helper functions

These do not generate other functions, just return values.


Generate a temporary filename (under $TMPDIR or /tmp) with given prefix.


Set this variable to the logging function that will be called before each run and cd. Set it to function(){} to turn off logging. It's console.log by default.