🔄 Using nodemon for Automatic Server Restart in Node.js

Introduction

During development, restarting your Node.js server manually after every code change can be tedious. nodemon is a popular development tool that automatically monitors your project files and restarts the server whenever changes are detected, making the development workflow faster and more efficient.

Information

nodemon is intended for development only. It should not be used to run applications in production.

What is nodemon?

nodemon is a command-line utility that wraps your Node.js application. It watches for file changes and automatically restarts the application whenever source files are modified.

👨‍đŸ’ģ Edit Source Code
💾 Save the File
👀 nodemon Detects Changes
🔄 Stops the Current Server
🚀 Restarts the Application Automatically

Installing nodemon

Install as a Development Dependency

Installing nodemon locally keeps it available only for your project and ensures everyone working on the project uses the same version.

Install nodemon locally

npm install --save-dev nodemon

Install Globally (Optional)

A global installation allows you to use the nodemon command from any project on your system.

Install nodemon globally

npm install -g nodemon

Tip

For team projects, a local development dependency is generally the preferred approach because it keeps project dependencies consistent across different environments.

Project Structure

📁 my-node-app
📄 server.js
📄 package.json
📁 node_modules

Creating a Simple Server

server.js

const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, {
    "Content-Type": "text/plain"
  });

  res.end("Hello from Node.js!");
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Running the Application with nodemon

Instead of using the node command, start your application with nodemon.

Start the server

npx nodemon server.js

If nodemon is installed globally, you can run:

Run using global installation

nodemon server.js

What Happens Behind the Scenes?

â–ļī¸ Start nodemon
Launches the Node.js application
Monitors project files
Detects file changes
Stops the running process
Starts a fresh server automatically

Adding a Development Script

Instead of typing the full command every time, define a script inside package.json.

package.json

{
  "scripts": {
    "dev": "nodemon server.js"
  }
}

Start the development server using:

Run development script

npm run dev

Watching Additional File Types

By default, nodemon watches JavaScript files. You can configure it to monitor additional file types such as JSON, TypeScript, or template files.

Watch multiple extensions

nodemon --ext js,json,ts server.js

Using a nodemon Configuration File

Create a nodemon.json file to avoid repeatedly specifying command-line options.

nodemon.json

{
  "watch": ["src"],
  "ext": "js,json",
  "ignore": ["node_modules"],
  "delay": "1000"
}

Configuration Options

OptionDescription
watchDirectories or files to monitor.
ignoreFiles or folders to exclude from monitoring.
extFile extensions to watch.
delayWait time before restarting after a change.
execCustom command used to execute the application.

Typical Development Workflow

🚀 Run npm run dev
âœī¸ Modify the code
💾 Save the file
🔄 nodemon restarts the server
🌐 Refresh the browser
✅ View updated output

Advantages of Using nodemon

  • ⚡ Eliminates manual server restarts.
  • 🚀 Speeds up development.
  • đŸ› ī¸ Easy to install and configure.
  • 📁 Watches multiple file types.
  • âš™ī¸ Supports configuration through nodemon.json.
  • đŸ’ģ Works well with most Node.js projects.

Common Commands

CommandPurpose
npx nodemon server.jsRun using the local installation.
nodemon server.jsRun using the global installation.
npm run devStart using the package script.
rsManually restart the server while nodemon is running.
Ctrl + CStop the running process.

Common Issues

nodemon Command Not Found

This usually occurs when nodemon is not installed globally or is not available in the system path. Using npx nodemon avoids this issue for locally installed versions.

Server Does Not Restart

  • Ensure the modified file is inside a watched directory.
  • Verify the file extension is included in the watch list.
  • Check that the file has been saved.

Frequent Restarts

If your application restarts too often, ignore generated files and build directories such as dist or node_modules using the ignore option.

Best Practice

Keep nodemon as a development dependency, define an npm run dev script, and use a nodemon.json file for projects that require custom watch behavior.

Comparison: node vs nodemon

Featurenodenodemon
Runs JavaScript files✅✅
Automatic restart❌✅
File watching❌✅
Development focused❌✅
Production runtime✅❌

Official Resources

nodemon GitHub Repository and nodemon Package on npm

Summary

Summary

nodemon is an essential development tool for Node.js applications. It monitors your project files, automatically restarts the server after changes, reduces repetitive manual work, and significantly improves the development experience. Combining nodemon with an npm run dev script and a nodemon.json configuration provides a clean and productive workflow.