Especially during development, it’s useful to have your Node.js application automatically restart when files change on disk. We saw a similar technique work well when running tests using Mocha back in Enabling Continuous Testing with Mocha.
Short for Node Monitor, nodemon runs a Node.js program and then automatically restarts it whenever the source code changes or if the process terminates. To use it, first we have to install and save the dependency.
| | $ npm install --save --save-exact nodemon@1.11.0 |
Next, open your package.json file and add a new start command to the scripts section to override the default.
| | "scripts": { |
| | "start": "nodemon server.js", |
| | "test": "echo \"Error: no test specified\" && exit 1" |
| | }, |
After you save the package.json file, run npm start again from your terminal.
| | $ npm start |
| | |
| | > b4@1.0.0 start ./code/web-services/b4 |
| | > nodemon server.js |
| | |
| | [nodemon] 1.11.0 |
| | [nodemon] to restart at any time, enter `rs` |
| | [nodemon] watching: *.* |
| | [nodemon] starting `node server.js` |
| | Ready. |
From here out, you shouldn’t have to stop and rerun npm start as you make changes to the files in the B4 project. nodemon will faithfully watch for file changes and restart the process for you.