Automatically Restart Node.js During Development

Recently I have been attempting to learn at least a little bit about Node.js development. It only took about 15 minutes before getting completely sick of hitting Ctrl+C, the arrow up key, and Enter to restart the simple Node.js application I am working on.

Thankfully I have been fiddling with Grunt recently as well. A quick search revealed an NPM module perfectly designed to watch for file changes and restart a specified Node.js instance, grunt-nodemon. To use this nifty little module, start by getting the NPM modules you will need.

npm install grunt grunt-nodemon

Next, create a file named Gruntfile.js in this directory. Populate this new file with the following.

This file is pretty simplistic. It assumes you are creating your application in a child directory called app and your point of entry script is index.js. Invoking the script is accomplished by entering the following at the command line.

$ grunt serve

There are a whole lot more options available in grunt-nodemon. However, this simple script saves a whole boatload of irritating steps when learning Node.js through various tutorials and having to constantly stop, restart and test the server. Hopefully, if you are finding this, it makes your life a little easier as well.