Nodemon for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Nodemon for JavaScript (Node.js): Complete Guide with Examples

1. What Is Nodemon and Why Use It

Nodemon is a development utility for Node.js that automatically restarts your server (or any Node.js application) whenever it detects file changes in your project. It's an essential tool for web developers working with Express, Koa, Fastify, and other frameworks. Instead of manually stopping and restarting your server after every code change, Nodemon does it for you by monitoring the file system and reacting to file save events.

The main goal of Nodemon is to speed up your development workflow. Without it, developers waste time on repetitive actions: Ctrl+C, up arrow, Enter. With Nodemon, this process is fully automated. It doesn't just restart the process — it also cleanly terminates the previous instance, freeing up ports and resources. Additionally, Nodemon can ignore specific files and folders (like node_modules or .git), reducing CPU and disk load.

It's important to understand that Nodemon is not a library in the traditional sense (like Lodash or Axios), but a CLI tool (Command Line Interface). It's not meant to be imported into your code as a module (though technically possible via its API). Its primary use is running from the command line or through scripts in package.json. Nodemon supports all operating systems (Windows, macOS, Linux) and works with any version of Node.js.

2. Installation

Nodemon is installed via npm (Node Package Manager). It's recommended to install it globally or as a dev dependency in your project.

Global installation (for all projects):

npm install -g nodemon

Local installation (for a specific project):

npm install --save-dev nodemon

After local installation, you can run Nodemon via npx or by adding a script to package.json:

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

Then run: npm run dev.

3. Quick Start — Minimal Working Example

Create a file app.js with a simple HTTP server:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World!\n');
});

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

Now run Nodemon:

nodemon app.js

You'll see output like:

[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js`
Server running at http://localhost:3000/

Try changing the text in res.end() to 'Hello, Nodemon!\n' and save the file. Nodemon will automatically restart the server, and you'll see the updated response when you refresh the page in your browser.

4. Main Methods/Functions/Classes

Nodemon provides both a CLI interface and a programmatic API. Below are the main methods and features with examples.

4.1. nodemon([options]) — Programmatic Launch

Signature: nodemon([options: Object])

What it does: Launches Nodemon programmatically from a Node.js script. Allows fine-tuning behavior and handling events.

Example:

const nodemon = require('nodemon');

nodemon({
  script: 'app.js',
  ext: 'js json html',
  ignore: ['node_modules/', 'test/'],
  env: { NODE_ENV: 'develop                    

Recommendations