Reload Express.js application on file save

When you start with Node.js and Express you are probably friends with

  • CTRL + S (Save file)
  • CMD + Tab (Mac, switch windows)
  • CTRL + C (Stop server/application in terminal)
  • UP ARROW then ENTER (Restart server/application)

However there are tools to do this for you, one is Nodemon, and you can install this globably with

npm install -g nodemon

For your standard Node.js app which you start with node server.js you change that call to nodemon server.js. Now, each time you make a change Nodemon will handle restarting the app ... think of it frantically calling CTRL+C, UP ARROW & ENTER in the background for you.

For Express.js the setup is slightly different. From the terminal you would use something like this (Mac/Linux) npm start or DEBUG=myApp npm start to get your project up and running on http://localhost:3000. To use Nodemon here you need to edit your projects package.json file so it looks like this

"scripts":{ // "start": "node ./bin/www" "start": "nodemon ./bin/www" }

Once that's done, start your Express.js project the normal way and each time you make a change you can go straight to the browser and refresh the page.