Nodemon—A Brief Introduction
During the Node.js development process, developers are required to make several changes in the running code. To make the updates take effect, the Node.js server needs to be restarted repeatedly. In a production environment, performing such arduous tasks can be a bit overwhelming for the developers.
However, a possible solution to automate this process and eradicate the need to manually restart the server on each code update is Nodemon. It is a CLI tool made by @rem to automatically restart the application server whenever the code is updated. Serving the purpose under the motto of “reload, automatically,” Nodemon can efficiently ease out the development process.
In this Node.js instructional exercise, we will discuss the installation and configuration process of Nodemon to auto-restart the Node.js web application.
Installation
Before installing Nodemon, having Node.js installed on the server is a mere prerequisite. You should install Node first and then start off with this tutorial.
Step 1
To install Nodemon on your machine, you can either opt for a global or local installation process using Yarn or NPM. Here, we will discuss both criteria with both NPM and Yarn.
Local Installation
You can install Nodemon locally with NPM or Yarn by introducing Nodemon as a dev dependency with - save-dev (or - dev):
Command -
Or, conversely, with yarn:
Command -
One thing to know about with a local installation is you will not have the option to utilize the Nodemon directly from the command line. When done so, you will encounter the below-mentioned error:
Error - command not found: nodemon
However, you can utilize it as a feature of some npm scripts or with npx.
Global Installation
To install Nodemon globally using npm:
Command -
Or, using Yarn:
Command -
Step 2
Let us launch a Node.js sample file (app.js name) that is created on an Express server. We can begin with the command following:
Command -
Or we can pass the port number to run the server too in the command as:
Command -
We know that for every change in the server files with extension .js, .coffee, .mjs, etc., Nodemon will automatically restart the server and show the respective output. Let us assume the contents of the NodeJS file (app.js) are:
And start the app.js file by following the below-mentioned command:
Command -
Upon successful rundown, we will see the following output:
Output -
Now, while the server is still running, to produce a message “Hello World! This is my first Node.js project with Nodemon,” the contents of the app.js file should be:
You will notice some changes happening automatically in the terminal section. These would be due to updates done in the app.js file, and Nodemon will automatically sense those updates in files and restarts the server.
Output -
Alternatively, you can simply start the server by passing the Nodemon command only if your package.json file includes the “main” attribute (depicting the main server file) and the start script for a node under the “scripts” attribute.
Your package.json will look as follows:
Step 3
However, Nodemon comes with several added functionalities too. You can have a list of all by inputting the “nodemon -h” command. Some of the most common among them are discussed below:
- --verbose: A verbose output includes information on what file(s) has been modified to reboot.
- --watch: To specify several directories or files to be monitored by Nodemon, use the --watch switch. Add a --watch option for each directory to be watched.
- --ext: Specify multiple file extensions that Nodemon can monitor (e.g., --ext .coffee, .js, .py, .ts).
- --exec: To provide a binary to run the file with, use the --exec switch.
- --delay: Used to set a different delay with the --delay switch.
- --ignore: You may use the --ignore option to ignore files, patterns, or directories.
Step 4
Although specifying these configurations for Nodemon can appear to be easy. However, the opposite is true in the real production environment.
Developers are usually overwhelmed by tasks and functionalities already; they can’t really devote their energy to manually provide configuration to Nodemon. Thus, Nodemon has an interesting way around using the nodemon.json file. Inside this file, the developer has to specify the configurations only once and can be used repeatedly. Let's explore a sample nodemon.json file to gain a clear perspective on this:
Conclusion
In this tech explainer, we have learned about how we can automate our Node.js application using a CLI tool named Nodemon. While using Node.js, users have to restart the main server manually to get the changes to reflect. Nodemon eliminates the need for users to manually restart the server on any code change. It can restart the application on its own as soon as it encounters some changes in the code files.