George Shank

Software and stuff

Need a Node.js, MongoDB, or Bluetooth LE expert?
Drop me a line!

When you’ve got a somewhat complex Node app going you can end up with a lot of necessary batch operations you need to run before actually running your app. Some people run them manually and others actually write a small script that does this for them. Either way it’s a little disconnected from actually running the app.

It turns out NPM actually has an amazing solution for this. NPM supports a scripts setting in the package.json where you can configure many items, among them being start and prestart. You simply add a command line action you’d like performed before starting the app in the prestart tag and then node app.js in the start tag and you’re ready to rock and roll. All you have to do is npm start and you’re off to the races.

I have some coffeescripts I need transpiled before each run so my package.json looks a little like this:

  "name": "app-name",
  "author": {
    "name": "George Shank"
  "version": "0.1.1-3",
  "dependencies": {
    "express": "2.5.9",
    "jade": ">= 0.0.1"
  "scripts": {
    "start": "node app.js",
    "prestart": "coffee -c public/js/*.coffee"

There’s a ton of potential here. You could do this with all your Less files as well or anything else you need done just before run time.

Happy hacking!

Thanks to @supershabam for showing this to me.