Sunday, June 14, 2020

Restart Your Next NodeJS App Automatically Using Nodemon

We all know Node's asynchronous I/O has made it one of the de-facto tools for developing services such as Rest Apis. If you have worked on any big projects, you may have experienced a massive problem of manually restarting the server again after making changes to the code.

This practice of making changes and restarting the server repeatedly is actually very irritating and painful for the developers.

In this article, I got a solution for all the node.js developers, i.e., an npm package named Nodemon.

What is Nodemon?

You started your server, and it's running successfully. Now you open your browser to test it, and you suddenly notice some error or content is not showing as you expected. What will you do here? Go to the code, make changes, and then manually restart the server again, right?

Doing that one or two times is entirely okay, But what if you need to do this repeatedly?. It may start to irritate you. Here comes the role of Nodemon.

Nodemon is a utility that monitors the changes in your NodeJs application and automatically restarts the server, saving your precious time and tedious work.

How to use Nodemon?

In the following steps, I will teach you how you can configure Nodemon in your project and run your server.

Steps 1

Configure your node project and install nodemon in your app
 npm install -g nodemon  

The nodemon will be installed globally to your system path.

 npm install --save-dev nodemon  
If you want the nodemon to be installed as a development directory, i.e., in the current project only.

Step 2

Update your package.json file by updating your start script, as shown.

 "scripts": {  
   "start": "node app.js" //this is updated  

Step 3

Open your command prompt or console and navigate to the directory.
Then simply run the command 'nodemon', and your server is up running.
Now, if you make any changes to the code. The server will automatically start without your indulgence.
Note: If your server was already running, then close it first using ctrl+c and then start it using nodemon.

For more, you can visit the documentation of nodemon here.


Time to look at what we learned, Nodemon is a utility that monitors the changes to the code made in your file, and then we learned how we can use nodemon in our app,

If some of you had a question like how we can run the server automatically or how to automate the server in Node.js, then you probably have got your answer.

Will you use it in your next project?
Previous Post
Next Post

post written by:

Hi, Navjyot Singh is a coder, content maker and a freelance developer who's pursuing an undergraduate Engineering degree in Computer Science. He started out as a web developer but later picked up the mobile as his favourite platform to develop applications. A Writer by day and coder by night is loathed to discuss himself as the third person but can be persuaded to do so from time to time.