paint-brush
Setup Nodemon to auto restart Nodejs application serverby@amanhimself
11,350 reads
11,350 reads

Setup Nodemon to auto restart Nodejs application server

by Aman MittalAugust 11th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Manually restarting Node.js application is a tiring and tedious job. <a href="https://www.npmjs.com/package/nodemon" target="_blank">Nodemon</a> is the best solution available to autorestart a nodejs app server in development mode.
featured image - Setup Nodemon to auto restart Nodejs application server
Aman Mittal HackerNoon profile picture

Manually restarting Node.js application is a tiring and tedious job. Nodemon is the best solution available to autorestart a nodejs app server in development mode.

Step 1

Organize the source directory src and initiate it with an app.js or index.js or server.js or any other convention you use to bootstrap a Node.js server.

Update the package.json file accordingly by adding a start script.

Step 2

Add express or any other framework as dependency to bootstrap a minimal server.

Code for a minimal server:

In first terminal window start the server:

In second terminal window, request the url to test if the api is working and to see the response message:

Now if I change the response message, I have to restart the server to get the desired result:

Use Ctrl + C to stop the currently running server and restart it by using the same command before: npm run start.

Using the curl command again from terminal window we get the desired result:

This whole process is repetitive will slow your development of any package or application. Better solution is to use nodemon.

Step 3

Add nodemon as devDependency:

Step 4

Make another script dev under npm scripts in package.json file:

Now run $ npm run dev and request using curl command, we will see the last familiar result:

If I change the response message in index.js file back to Hello World, this time I don't I have to restart the server since nodemon is watching for the changes using inside the src directory, through its --watchparameter. If I use the curl command again, the result is familiar with the update

One can verify by observing the log messages in the terminal window where nodemon is running:

To stop the nodemon process, use Ctrl + C.

Full Source at this Github Repository.

If you find this article useful, please clap 👏 .

Thank you!