Nodemon In Node.Js

As Nodemon suggests: 
Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. It's perfect for development.
The scenario is that, when you are working with Node.js server, every time any changes occur you should start the server again and again.  I have the solution for that; it's none other than Nodemon, which is a great approach.
By using Nodemon you can achieve high performance in production using automative restart of a server without any hassle . 
It continuously monitors your directory or file system , when any changes are found it automatically restarts the node application server .
Nodemon is an NPM package, so by installing the package you will learn more about it. 
Some of the features for Nodemon
  • Automatic restart for application 
  • Open source 
  • Useful for work efficiency
  • Continious watch on current working directory
  • Can ignore specific type of files
  • Suitable for nodejs applications 
How to use Nodemon with nodejs 
Don't worry at all, it's pretty easy to use the Nodemon package inside your nodejs application. 
To get started with Nodemon , you need to install the package globally as described below
  1. npm install -g nodemon  
After excecuting the above command, the nodemon package will be installed on a global location so we can use  itwhenever we want in our other applications. 
iI you are installing Nodemon locally and trying to use it,  than you may face an error ssuch as:
" The term 'nodemon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
For that you can specify that when you are serving your request, Nodemon will be executed as described below:
package.json snippet 
  1. {  
  2.   "name""nodemondemo",  
  3.   "version""1.0.0",  
  4.   "description""This is demo about how to use nodemon with nodejs",  
  5.   "main""MyApp.js",  
  6.   "dependencies": {  
  7.     "nodemon""^1.17.1"  
  8.   },  
  9.   "devDependencies": {},  
  10.   "scripts": {  
  11.     "test""echo \"Error: no test specified\" && exit 1",  
  12.     "serve" : "nodemon MyApp.js"  
  13.   },  
  14.   "keywords": [  
  15.     "nodejs",  
  16.     "nodemon",  
  17.     "in",  
  18.     "node.js"  
  19.   ],  
  20.   "author""Manav Pandya",  
  21.   "license""MIT"  
  22. }  
Place serve value inside scripts section as specified :
So far  we have learned easy steps for how to install and use nodemon for global location as well as local location
Let us see how Nodemon works .
I'm going to use the Nodemon package globally, rather than locally.
Created Myapp.js file and the snippet will be:
  1. // Using Core node.js package HTTP  
  2. var http = require('http');  
  4. // Creating HTTP Server  
  5. var server = http.createServer(function(req, res) {  
  6. res.writeHead(200);  
  7.     res.write('<h1>C#Corner - Nodemon with NodeJs</h1>');  
  8.     console.log("Writing In Console !!!");  
  9. });  
  11. // Specifying Custom Port Number  
  12. server.listen(5555 , function()  
  13. {  
  14.     console.log("Server started on port : http://localhost:5555");  
  15. }  
  16. );   
Now just open command line or console and write a simple command 
  1. nodemon MyApp.js  
As you can see I  wrote Nodemon instead of node MyApp.js. This is the way you can run the same node app using this command. 
If you are stuck somewhere, it's ok , just go to the command line and ask for help. 
  1. nodemon -h  
It will show you the complete help attributes as shown below :
It's time to execute our node application at the specified url :
After opening any browser, you can see the output like this:
To see how Nodemon actually restarts our app, let's change any part of a snippet.
I have added a single line inside Myapp.js file as below 
  1. // Using Core node.js package HTTP  
  2. var http = require('http');  
  4. // Creating HTTP Server  
  5. var server = http.createServer(function(req, res) {  
  6. res.writeHead(200);  
  7.     res.write('<h1>C#Corner - Nodemon with NodeJs</h1>');  
  8.     res.write('<h3>Nodemon is utility that is used to monitor any changes in our source code .</h3>');  
  9.     console.log("Writing In Console !!!");  
  10. });  
  12. // Specifying Custom Port Number  
  13. server.listen(5555 , function()  
  14. {  
  15.     console.log("Server started on port : http://localhost:5555");  
  16. }  
  17. );   
.So I have added an extra line of text to mark as changed in my javascript file 
Now when actually saving my file , the application is going to refresh itself using Nodemon. You can see in the console that [nodemon] actually restarted server by watching a few changes inside the current working directory (in my case it's a single file).
 As you can see in the above screen , when I'm going to save my changes it will be automatically reflected in the existing output. 
So now my application is refreshed and the changes done by me are now reflected on the existing output. 
How to specify multiple directories to mark changes. 
A situation may occur like we want to mark specific directories or files being monitored by a utility.
Nodemon covers the complete existing/working directory for any kind of changes, either of single dot or thousands of lines inside a snippet.
We can specify directory to be marked for changes with --watch keyword like:
  1. nodemon --watch demodirectory/app.js --watch MyApp.js  
I have included one new directory, and also added a single file inside parent directory. Refer to the below screen to get an idea about my customized directory structure
If you want to know if a  specified directory is marked for changes or not, I've created a new directory named "ignoredirectory" , and inside this directory I've created new file called newapp.js. The code will be like below: 
  1. // This file changes not gonna be reflect  
  3. Response.write("Ignoring This File For Any Changes !!!");  
So when iIm saving this file, node server is not going to restart.
This way you can specify a directory for changes to be reflected. 
Inspecting with Nodemon 
With Nodemon, you can also inspect you application. For that you can use  --inspect  followed by script file name:
  1. nodemon --inspect MyApp.js  
And in the console you can see that debugger is already started at port number :
To learn that how to inspect a node.js application you can follow the link given below:  
We have covered so many things in this article:What is Nodemon in Node.js
  • How to get started with nodemon, and installation as well
  • How nodemon works 
  • Specify multiple directory for changes to be marked by nodemon
  • Inspecting node app with Nodemon
I hope you found this article useful in some way. Thanks for the read.