nodemon

nodemon is a lovely little tool for watching for changes to node.js source code and automatically restarting your server during development.

There are a number of solutions available for restarting your node.js server when you update your source code (e.g., forever, up, etc.). Some of these have advanced features like running as a background service and load balancing that are useful in a production environment. However, the simplest tool I’ve found so far for development use is Remy’s nodemon.

To install:

npm install -g nodemon

Then run:

nodemon my-server.js

That will get it to run my-server.js and also watch the folder that the script is in in for changes to any of the files. When a file does change, it will restart your server using the latest code. Of the similar tools I’ve tried, nodemon was the fastest to react to changes (hey, those precious seconds add up when you’re continuously testing your scripts).

Did someone say ‘CoffeeScript?’

As an awesome little side benefit, nodemon can also directly run and watch a server written in CoffeeScript:

nodemon my-server.coffee

One thing I find valuable when working with CoffeeScript (e.g., via CodeKit) is being able to quickly refer to the generated JavaScript file. You can’t do this with nodemon alone, however, as it doesn’t save the compiled JavaScript (it would have to call the CoffeeScript compiler twice, once to compile it and once to run the server. I thought about adding this to nodemon but it needlessly complicates the code for an edge case.) Instead, you can easily achieve this by having two Terminal windows open and running the CoffeeScript compiler in one and nodemon in the other:

coffee --compile --watch my-server.coffee
nodemon my-server.coffee

This will save my-server.js and restart the server any time you update your code.

By the way, if you happen to find yourself experiencing multiple restarts when you make a change because nodemon is picking up the generated JavaScript as a changed file, just add those files (or your output folder) to your .nodemonignore file.