Introduction
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
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.
Note
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."
Solution:
For that you can specify that when you are serving your request, Nodemon will be executed as described below:
package.json snippet
- {
- "name": "nodemondemo",
- "version": "1.0.0",
- "description": "This is demo about how to use nodemon with nodejs",
- "main": "MyApp.js",
- "dependencies": {
- "nodemon": "^1.17.1"
- },
- "devDependencies": {},
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1",
- "serve" : "nodemon MyApp.js"
- },
- "keywords": [
- "nodejs",
- "nodemon",
- "in",
- "node.js"
- ],
- "author": "Manav Pandya",
- "license": "MIT"
- }
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 .
Note
I'm going to use the Nodemon package globally, rather than locally.
Created Myapp.js file and the snippet will be:
-
- var http = require('http');
-
-
- var server = http.createServer(function(req, res) {
- res.writeHead(200);
- res.write('<h1>C#Corner - Nodemon with NodeJs</h1>');
- console.log("Writing In Console !!!");
- });
-
-
- server.listen(5555 , function()
- {
- console.log("Server started on port : http://localhost:5555");
- }
- );
Now just open command line or console and write a simple command
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.
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
-
- var http = require('http');
-
-
- var server = http.createServer(function(req, res) {
- res.writeHead(200);
- res.write('<h1>C#Corner - Nodemon with NodeJs</h1>');
- res.write('<h3>Nodemon is utility that is used to monitor any changes in our source code .</h3>');
- console.log("Writing In Console !!!");
- });
-
-
- server.listen(5555 , function()
- {
- console.log("Server started on port : http://localhost:5555");
- }
- );
.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:
- 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:
-
-
- 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:
- nodemon --inspect MyApp.js
And in the console you can see that debugger is already started at port number : 127.0.0.1:9229
To learn that how to inspect a node.js application you can follow the link given below:
Conclusion
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.