Nodemon: The Lifesaver for Node.js Developers – No More Manual Restarts!

 


Imagine you're coding away, making awesome changes to your Node.js app, and then… BAM! You have to restart the server manually just to see the changes. 

That's like baking cookies  but having to restart the oven every time you add a new ingredient!

Enter Nodemon – your automated assistant that restarts your server every time you save a file. No more Ctrl + C and node server.js nonsense! 

Let’s explore how to install and use Nodemon, plus its powerful options to supercharge your development workflow! 🚀

1. What is Nodemon and Why Should You Use It?

Nodemon is a tool that automatically restarts your Node.js application whenever you make changes to the source code.

Why is Nodemon awesome?

  • Saves time – No more manual restarts!
  • Boosts productivity – Keeps your development flow smooth.
  • Works with CommonJS (CJS) and ES Modules (ESM).
  • Customizable – You can control what files to watch or ignore.

2. How to Install Nodemon in Node.js

There are two ways to install Nodemon:

Install Nodemon Globally (Use It Everywhere)

npm install -g nodemon
  • Use nodemon from anywhere in the terminal.
  • Best if you want Nodemon for multiple projects.

Install Nodemon Locally (Per Project)

npm install --save-dev nodemon
  • Installed only in the current project.
  • Best for team projects (ensures everyone uses the same version).

3. How to Use Nodemon in Node.js (Basic Setup)

Instead of running your app with:

node server.js

Run it with:

nodemon server.js

Now, every time you save changes, the server restarts automatically! 

4. Using Nodemon in CommonJS (CJS) and ES Module (ESM)

Nodemon works with both CJS (require) and ESM (import).

Nodemon with CommonJS (CJS)

If your project uses CommonJS (require), create a server.js file:

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Hello, Nodemon is running!");
});

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

Run:

nodemon server.js

Nodemon with ES Module (ESM)

If your project uses ES Modules (import), first enable ESM by adding "type": "module" in package.json:

{
  "type": "module"
}

Create a server.mjs file:

import express from "express";

const app = express();

app.get("/", (req, res) => {
  res.send("Hello, Nodemon with ESM!");
});

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

Run:

nodemon server.mjs

Nodemon will now restart your ESM server automatically!

5. Customizing Nodemon with nodemon.json

You can configure Nodemon by creating a nodemon.json file in your project root.

Example configuration:

{
  "watch": ["server.js"],
  "ext": "js,json",
  "ignore": ["node_modules/", "public/"],
  "exec": "node server.js"
}

What does this do?

  • watch – Monitors changes in server.js only.
  • ext – Only restarts on .js and .json changes.
  • ignore – Ignores changes in node_modules/ and public/.
  • exec – Runs node server.js automatically.

Run Nodemon as usual:

nodemon

Now, Nodemon follows your custom rules!

6. Useful Nodemon Command-Line Options

Nodemon has several powerful options you can use:

Option Description
-w or --watch Watch specific files or folders (nodemon -w server.js).
-e or --ext Monitor specific file extensions (nodemon -e js,json).
--ignore Ignore specific files or folders (nodemon --ignore logs/).
--exec Run a custom command (nodemon --exec babel-node server.js).
--delay Add a restart delay (nodemon --delay 3 server.js).
--verbose Show detailed logs (nodemon --verbose server.js).

Example: Watch Only server.js and Ignore logs/

nodemon -w server.js --ignore logs/

Now Nodemon will restart only when server.js changes and ignore logs!

7. Running Nodemon as an NPM Script

Instead of running nodemon server.js every time, you can add a script in package.json:

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

Now, just run:

npm run dev

Much easier and cleaner!

8. Using Nodemon with TypeScript (Bonus!)

If you’re using TypeScript, you need ts-node:

npm install -g ts-node nodemon

Run Nodemon with:

nodemon --exec ts-node server.ts

Now, your TypeScript server restarts automatically! 

9. Why Developers Love Nodemon

No more manual restarts – Nodemon does it for you!
Works with both CJS and ESM – No compatibility issues!
Highly configurable – Watch only what you need.
Perfect for development – Saves time and effort.

Now you can focus on coding while Nodemon takes care of the rest! 

#HappyCoding! 

 

Post a Comment

0 Comments