Express.js Nodemon Auto Restart

Node.js reads the code once at startup. A change to a file does nothing until the server restarts. Stopping and starting the server after every small edit wastes time and breaks focus. Nodemon watches the project files and restarts the server the moment a file changes. Developers use it during development in almost every Express project.

The Automatic Door Analogy

A normal door needs a push every time. An automatic door senses a person and opens by itself. Nodemon turns the server into an automatic door. A saved file acts as the sensor, and the restart happens without any command.

How Nodemon Works

You edit and save app.js
          |
          v
Nodemon detects the file change
          |
          v
Nodemon stops the running server
          |
          v
Nodemon starts the server again
          |
          v
Browser refresh shows the new behavior

Install Nodemon

Nodemon belongs to development only. Install it as a dev dependency so production servers skip it.

npm install --save-dev nodemon

Add Scripts to package.json

Scripts give short names to long commands. The start script runs the plain server for production. The dev script runs the server under Nodemon.

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

Run the development server with the command below.

npm run dev

Try It With a Small App

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

app.get('/', (req, res) => {
  res.send('Hello, version one');
});

app.listen(3000, () => console.log('Server running on port 3000'));

Start the app with npm run dev and open http://localhost:3000. Change the text to Hello, version two and save the file. The terminal prints a restart message, and a browser refresh shows the new text.

Terminal Messages

MessageMeaning
starting node server.jsNodemon launched the app
restarting due to changesNodemon saw a saved file
clean exit - waiting for changesThe app finished and Nodemon waits
app crashed - waiting for file changesAn error stopped the app. Fix the code and save

Configure Nodemon

A nodemon.json file in the project root controls what Nodemon watches.

{
  "watch": ["src"],
  "ext": "js,json",
  "ignore": ["tests/*", "node_modules"],
  "delay": 500
}

watch

This option lists the folders Nodemon observes. A narrow list reduces needless restarts.

ext

This option lists the file extensions that trigger a restart.

ignore

This option names files and folders Nodemon skips.

delay

This option waits the given milliseconds before a restart. It groups quick consecutive saves into one restart.

Useful Commands

  • Type rs and press Enter in the terminal to restart by hand.
  • Run nodemon --inspect server.js to attach a debugger.
  • Run nodemon --exec "node --env-file=.env" server.js to load environment variables on newer Node.js versions.

Troubleshooting

Endless Restart Loop

The app may write a file inside a watched folder, such as a log file. Add that folder to the ignore list.

Command Not Found

A local install does not add nodemon to the system path. Run it through an npm script or use npx nodemon.

Changes Not Detected

Projects inside Docker or network drives sometimes miss file events. Add the legacyWatch option or the -L flag so Nodemon checks files by polling.

Best Practices

  • Keep Nodemon in devDependencies.
  • Use the start script with plain node in production.
  • Ignore test, log, and upload folders.
  • Pair Nodemon with a .env file for local settings.