Auto-Reload with nodemon

nodemon restarts your server automatically whenever you save a file.

Syntaxnodemon app.js

Normally you must stop and restart node app.js after every change. nodemon watches your files and restarts the server for you, which makes development much faster.

Setup

  1. Install it as a dev dependency: npm install --save-dev nodemon.
  2. Add a script to package.json.
  3. Run npm run dev.

Example

Example · json
{
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  }
}

When to use it

  • A developer installs nodemon as a dev dependency so the Express server restarts automatically every time they save a route file.
  • A team configures nodemon to watch only the src/ directory, preventing unnecessary restarts when test or documentation files change.
  • A nodemon.json config specifies a 500 ms delay before restarting so rapid consecutive saves do not trigger multiple restarts.

More examples

Install and run nodemon

Installs nodemon as a dev dependency and uses npx to launch it without a global install.

Example · bash
npm install --save-dev nodemon
npx nodemon index.js

Dev start script in package.json

Adds a 'dev' npm script that starts the server under nodemon, keeping 'start' as the plain Node production entry.

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

nodemon.json configuration file

A nodemon.json config that watches only src/, restarts on .js or .json changes, ignores test files, and waits 500 ms before restarting.

Example · json
{
  "watch": ["src"],
  "ext": "js,json",
  "ignore": ["src/**/*.test.js"],
  "delay": 500
}

Discussion

  • Be the first to comment on this lesson.