Examples


Express.js - Run With Nodemon


What is Nodemon?

Nodemon is a development tool for Node.js applications that automatically restarts your server when file changes are detected in the project.

1. Install Nodemon

You need to install Nodemon use the command is given below:

npm i nodemon

2. Modify package.json

In your package.json, add a script to start your server using Nodemon. This can be done in the scripts section.

"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js"
}

3. Run the Server with Nodemon

You need to install Nodemon use the command is given below:

npm run dev

Why Use Nodemon?

  • Watch your files for changes
  • Automatically restart the server
  • Speed up your development workflow

When building an Express or any Node.js app, you run this command:

node index.js

However, if you edit a file, you must manually stop and restart the server each time to see the changes. Nodemon resolves this.

npm run dev

Key Benefits

Feature What it Does
Auto restart Restarts server on file save
Watch multiple files Can watch .js, .json, .env, etc.
Better dev experience No need to manually restart the server after edits
Works with custom commands You can run scripts like babel-node, ts-node, etc.

Example of a simple Express app with Nodemon

1. Install Express: If you want to install express use the command is given below

npm install express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Welcome to Express.js!');
});

const PORT = 5000;

app.listen(PORT, () => {
    console.log(`Server is running on http:// localhost:${PORT}`);
});

Run the Server

Run the server using the command given below.

npm run dev
D:\my-app>npm run dev
Server is running on http://localhost:5000

Output

Express setup project output