| by Arround The Web | No comments

How to Automatically Restart Node.js Application?

Node.js is a well-reputed JavaScript run-time environment that is used for building dynamic, fast, and scalable web applications. During the development process, the developers need to make several changes in the running code from time to time based on the requirements. To make the updates affect, the developer needs to restart the Node.js server repeatedly.

The traditional restarting process starts the application by executing the “node<filename>” command in the terminal. To do this task repeatedly is time-consuming. However, the node application can be restarted automatically.

This post will explain how to automatically restart the Node.js application.

How to Automatically Restart Node.js Application?

Nodemon is a command line tool that monitors the Node.js project and resets the application automatically when any changes occur in it. It means that the user is not required to stop the Node.js application first and then start it again to take the changing effect.

Nodemon is easy to utilize and also does not require any instance to call it. Moreover, it does not affect the source code of the application and saves the time to execute it again and again by typing the “node<filename>”.

How to Install “nodemon”?

Node.js “nodemon” is an external module that the user needs to install globally in the project using the “-g” option. For this purpose, execute the below-stated command:

npm i -g nodemon

 
It can be seen that the “nodemon” is added globally into the current Node.js project:


Moreover, the “nodemon” can also be installed as a development dependency by using the following “–save” and “-dev” flags with the “npm” package manager:

npm i --save-dev nodemon

 
Now, the “nodemon” is added as a dependency in the current Node.js project:


Check “nodemon” Version

For more verification of “nodemon”, the user can run the below-stated command to check its installed version:

nodemon -v

 
In the above command, the “-v” flag denotes the “version” keyword.

The following output shows the installed version of “nodemon” which is “3.0.1”:

How to Use “nodemon” to Restart a Node.js Application Automatically?

To use the “nodemon” for restarting the Node.js application, specify it as a keyword followed by the file name in this way:

nodemon<filename>

 
In the above command the “filename” represents the “.js” file in which the source code of the Node.js application is written. For instance, its name is “app.js”.

Now, go through the given examples to use the “nodemon” practically to restart the Node.js application.

Example 1: Applying “nodemon” to Restart Node.js Application Initially

This example uses the “nodemon” to start the Node.js application without making any changes to it:

nodemon app.js

 
It can be analyzed that the “nodemon” started the “app.js” file successfully showing its output:


Example 2: Applying “nodemon” to Restart Node.js Application After Making Changes

This example uses the “nodemon” to restart the “app.js” file of the Node.js project after making the desired changes:

nodemon app.js

 
It can be observed that after saving the new changes in the “app.js” file, the “nodemon” automatically restarts it showing the output based on the updated content:


That’s all about restarting the Node.js application automatically.

Conclusion

To automatically restart the Node.js application, use the “nodemon” command line tool. This command line tool requires “npm(node package manager)” to install globally into the Node.js project. Once the installation is done, specify “nodemon” as a keyword with the “.js” file of the Node application as “nodemon<filename>” to restart it automatically for taking the updated effect. This write-up has practically explained how to automatically restart the Node.js application.

Share Button

Source: linuxhint.com

Leave a Reply