Using nodemon for Automatic Server Restart in Express.js

🚀 Introduction

During development, every change made to your Express.js application normally requires manually stopping and restarting the server. nodemon is a popular development utility that automatically restarts your Node.js application whenever it detects changes in your project files. This significantly improves the development workflow by providing instant feedback after every code modification.

Information

nodemon is a development tool and should generally be installed as a development dependency, not as a production dependency.

🤔 Why Use nodemon?

  • Automatically restarts the server after file changes.
  • Eliminates the need to manually restart the application.
  • Speeds up development and testing.
  • Works with Express.js, Node.js, and other JavaScript applications.
  • Simple to install and configure.

📋 Prerequisites

  • Node.js installed.
  • An Express.js project initialized with package.json.
  • Basic Express server created (for example, index.js).

🛠 Installation Methods

Installing nodemon locally makes it available only within the current project and is the recommended approach.

Install as a Development Dependency

npm install --save-dev nodemon

Installing globally allows you to use the nodemon command from any project on your computer.

Install Globally

npm install -g nodemon

Best Practice

Prefer the local installation because every developer working on the project will use the same version of nodemon.

📄 Updating package.json

Instead of running node index.js, create a development script inside the scripts section of package.json.

package.json

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

â–ļī¸ Running the Application

Start with nodemon

npm run dev

The server starts normally, but now nodemon continuously watches your project files. Whenever you save changes, it automatically restarts the server.

📝 Example Express Server

index.js

const express = require('express');

const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
  res.send('Server is running with nodemon!');
});

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

🔄 How nodemon Works

Developer
Edit Source File
Application Ready
Save File
nodemon Detects Changes
Stops Existing Server
Starts Updated Server

📌 Typical Development Workflow

📊 node vs nodemon

Featurenodenodemon
Automatic Restart❌ No✅ Yes
Built into Node.js✅ Yes❌ No
Development ToolBasic runtimeOptimized for development
Manual Restart Required✅ Yes❌ No

âš™ī¸ Optional Configuration

You can customize nodemon by creating a nodemon.json configuration file.

nodemon.json

{
  "watch": ["src"],
  "ext": "js,json",
  "ignore": ["node_modules"],
  "exec": "node index.js"
}

This configuration tells nodemon which folders to watch, which file extensions to monitor, which directories to ignore, and which command to execute when restarting the application.

💡 Best Practices

  • Install nodemon as a development dependency using --save-dev.
  • Use the npm run dev script during development.
  • Use node or npm start for production deployments.
  • Exclude unnecessary folders such as node_modules from file watching.
  • Store custom settings in a nodemon.json file for consistency across the development team.

Warning

nodemon is intended for development environments only. In production, use the standard node runtime or a production process manager such as PM2.

📚 Learn More

Visit the official resources for additional information:
â€ĸ nodemon Official Website
â€ĸ nodemon GitHub Repository
â€ĸ Express.js Official Documentation

📝 Summary

Summary

nodemon is a valuable development tool that automatically restarts your Express.js server whenever source files change. By installing it as a development dependency and running your application with npm run dev, you can streamline development, reduce repetitive tasks, and focus on building your application more efficiently.