Hot reload for node.js servers on code change

If you are working with node.JS, you may experience the need to restart the node server as soon as a file has changed.

There are plenty approaches for this field. One is, to parse for all require statements in the code and watch those files then.

I took a different approach, which could be described by this linux command:

$ find . | grep "\.js$"

It searches for all .js files in the folder and restarts the server, as soon as one of those changed. Very easy, but yet powerful.

To use it, just copy:

rundevserver.js into your server directory.

This script expects your server script to be called run_server.js. If you want to change it, just change the line.

Instead of running your server with

$ node run_server.js

you now start it with:

$ node run_dev_server.js

That's it!

This feature is of course part of the

spludo framework's 1.0-dev version. In spludo, you can use

$ bash run_dev_server.bash

instead of

 $ node run_server.js

to achieve a hot reload on each .js change.

Update 2010/08/27: Wilkerlucio created a version for hot reloading coffee-scripts.

In javascript, node.js, open source by
@ 04 Jul 2010, Comments at Reddit & Hackernews