Update Code Without Restarting Node Server

Whenever you are writing Node.js code on your system you have to run the “node index.js” command to execute the code written in the “index.js” file. Suppose you have made some changes to the “index.js” file and even after saving the file you will see there are no changes reflected in the application. After updating the code you have to manually close the server and start it again for changes to show. 

Each time you update your code you have to restart the server and this can be very annoying because we usually make a lot and lots of changes during development. This problem has a very simple solution which we will see in this article, so let’s get started.

Also Read: Node.js Debugging – Tools, Techniques, and Challenges

Install Nodemon

Nodemon is a development dependency that makes it super easy to restart the Node server, you don’t have to do anything manually. Nodemon automatically does it whenever you make any changes in the code. Nodemon module can be installed using NPM. 

Syntax of Installing Nodemon:

npm install nodemon

We recommend you install it globally so that you don’t have to manually install it on each NodeJS project directory.

Syntax of Installing Nodemon Globally: 

npm install -g nodemon

Verify the Installation:

You can verify that the Nodemon is successfully installed on your system by executing the below command in the command line.

nodemon --version

This will return a version of Nodemon if it is successfully installed.

How to use Nodemon?

To start a Node.js server we usually execute the below command. 

node serverFile.js

Now, when you want to use Nodemon, you have to replace node with nodemon

Below is the command to start Node using Nodemon: 

nodemon serverFile.js

where the serverFile.js is the file you wanted to execute with Nodemon, for instance, “app.js”, “index.js” or “server.js”.

Demonstrating Nodemon for Node.js Server Reloading

Let’s create a very simple express project to demonstrate the use of Nodemon.

Let’s create a folder “trynodemon” -> inside the folder create a file “index.js” -> open this file in any code editor -> create a basic app to print a simple message “Hello World!”.

index.js

// Just a simple hello world program

console.log("Hello World!")

Now open the command-line and run the below command to execute the above Node.js project using Nodemon:

nodemon index.js

Output(before making changes):

Output before making changes

This is an output of the current version of our application.

Output(after making changes):

Let’s try to make some code changes in the server files without restarting Node.js manually, let’s remove the word “World!” and then save the file.

Output after making changes

See the changes, Nodemon automatically restarted the server for us.

Manipulating Server Restart Time

Nodemon waits for one second to refresh the server whenever there are any changes in the file. If you want to change this default time to something else, you can use the –delay flag.

Below is the command for Manipulating Restart Time:

nodemon –delay secs

For example, If you run the command “nodemon –delay 2”, it will wait for 2 seconds after the file changes to refresh the server.

Manipulating restart time

Restart manually without stopping Nodemon

You can even restart a server manually in case Nodemon is not restarting properly by typing rs on the terminal and pressing enter.

Restart manually without stopping nodemon

Conclusion

Node.js is mainly used to create the backend of websites, microservices and APIs. Each of them is required to run a server continuously. When a NodeJS server and any other NodeJS code are running and the code is changed, the changes are not reflected in the Node.js app until you restart the server.

In this article, we have seen how to automatically restart the NodeJS server whenever you update the code. We have used the Nodemon for that. To use Nodemon you just have to install it using npm and then replace the “node” keyword with “nodemon” whenever run a Node app. The rest of the thing Nodemon will handle automatically. Hope you have enjoyed reading the article. 

Read More:

Reference 

https://stackoverflow.com/questions/2925940/how-can-i-edit-on-my-server-files-without-restarting-nodejs-when-i-want-to-see-t

Aditya Gupta
Aditya Gupta
Articles: 113