Restart Your Node.js server automatically using nodemon

Introduction:

In your Node.js apps, you have to restart your server every time you make changes to your project. This constant rebooting of a Node.js application manually is a very tedious and tiring job, to avoid this we can use Nodemon. Nodemon is one of those tools which will automatically restart your node application when file changes in the directory are detected.

nodemon will not make any additional changes to your code or method of development.

In this article, you will learn how to set up nodemon.

Prerequisites:

  • Install Node.js

Installation:

npm install -g nodemon

The above line of command will install nodemon globally to your system path.

npm install --save-dev nodemon

You can also install nodemon as a development dependency using the above line of code.

Setting up:

You need to add a line(line 7) in the package.json file:

{
  "name": "restful-blogapp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "dev": "nodemon ./app.js localhost 6060"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "ejs": "^3.0.1",
    "express": "^4.17.1",
    "express-sanitizer": "^1.0.5",
    "method-override": "^3.0.0",
    "mongoose": "^5.8.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

Now if you want to run your node project. All you need to do is type the below command:

npm run dev

This will start the server in development mode.

We see the following terminal output:

Now if you make changes to the file, nodemon will automatically start your server.

 

Leave a Reply

Your email address will not be published. Required fields are marked *