Skip to main content
How to Use Nodemon to Automatically Restart Node.js Applications
6 min read

How to Use Nodemon to Automatically Restart Node.js Applications

Update: Enhanced Watch Mode in Node.js Version 22

One of the standout features in the latest release of Node.js, version 22, is the improved watch mode. This feature is now considered stable, which means it's no longer experimental and can be relied upon for regular use.

Watch mode simplifies developers' lives by automatically restarting the Node.js process whenever it detects changes in the files being monitored. This is particularly useful if you're working on large projects where manual restarts can be tedious and time-consuming.

Previously, developers might have used tools like Nodemon and Watchman to manage file changes, especially on Windows, where detecting these changes could be challenging. Watch mode offers a more integrated and straightforward approach, eliminating the need for external tools and streamlining the development process.

By using watch mode, you no longer need to manually restart your server or tools like Nodemon after making changes to your code. This saves time and allows you to see the effects of your changes immediately, making your development workflow much more efficient.

Introduction

As a developer, you know how tedious it is to restart the application every time you tweak the code. Nodemon eliminates this hassle by automatically restarting the app whenever any change is detected. It's incredibly helpful as it lets you concentrate on coding without interruption.

Nodemon watches the files in your project and instantly applies updates by restarting your app—no more manual resets. This has noticeably sped up my development process, making it smoother and more productive. It’s especially crucial in a fast-paced work environment where every second counts.

Installing Nodemon

I just went through setting up Nodemon. You install it using npm, the Node.js package manager. Just open your terminal and type:

npm install -g nodemon

This installs Nodemon globally on your system, so you can use it for any project. To make sure it's installed correctly, you can check its version with:

nodemon --version

If it shows the version, you're all set! Nodemon will now automatically restart your Node.js applications whenever you make changes. It’s a real time-saver, making it smoother to develop without breaking your flow.

Using Nodemon

Here's a simple Express.js application example using Nodemon:

// 1. First, install the Express.js and Nodemon modules
const express = require("express");
const app = express();

// 2. Define a simple endpoint to listen for HTTP requests
app.get("/", (req, res) => {
res.send("Hello, World!");
});

// 3. Start listening for the application on a specific port
const port = 3000;
app.listen(port, () => {
console.log(`Server successfully connected to port ${port}.`);
});

This represents a basic Express.js application. The main file can be named app.js, for example. Now let's demonstrate how to use Nodemon to automatically restart this application.

First, make sure you're in your project directory where your Node.js application, like an Express.js app, is located.

  1. Install Express.js and Nodemon if you haven’t already.
npm install express nodemon
  1. After installing Nodemon, using it to automatically restart your Node.js apps is simple. Navigate to the directory where your Node.js application is located using the terminal or command prompt. Once you're in the project directory, you can start your Node.js application with Nodemon by typing:
nodemon app.js

This command fires up your application with Nodemon, so it automatically restarts whenever you change any files. For instance, if you update the response in your app.js from 'Hello, World!' to something else and save, Nodemon immediately restarts and applies the update without you needing to do anything.

Also, here’s what you typically see in the terminal when you start:

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,json
[nodemon] starting `node app.js`
Server successfully connected to port 3000.

It shows Nodemon is watching all your files and is ready to restart anytime you make edits.

In this output:

  • [nodemon] 2.0.15: This indicates the version of Nodemon being used.
  • [nodemon] to restart at any time, enter 'rs': This message informs you that you can manually restart the application at any time by entering 'rs' in the terminal.
  • [nodemon] watching path(s): *.*: Nodemon is watching for changes in all files and directories.
  • [nodemon] watching extensions: js,json: Nodemon is specifically watching for changes in JavaScript and JSON files.
  • [nodemon] starting 'node app.js': Nodemon is starting the Node.js application (app.js in this case).
  • Server successfully connected to port 3000.: This message indicates that the Express.js server has successfully started and is listening on port 3000.

Advanced Configuration

I’ve been diving deeper into Nodemon’s capabilities and found some cool ways to customize its behavior to better suit our projects. Nodemon allows you to tailor its restart rules according to our specific needs, which is super handy.

Here’s a breakdown:

  1. File Watch Patterns: You can set Nodemon to watch only specific directories. This is great because it lets us focus on crucial parts of our project without getting distracted by unnecessary restarts.

    For instance, if you only want to watch the src and config directories, your config would look like this:

    {
    "watch": ["src", "config"]
    }
  2. Ignore Patterns: We can also tell Nodemon to ignore certain directories that don’t impact our app’s functionality, like log files or public assets. This means Nodemon won’t restart when changes occur in these areas.

    Here’s how you could set it up to ignore the logs and public directories:

    {
    "ignore": ["logs", "public"]
    }
  3. Delaying Restarts: If rapid successive changes are causing too many restarts, we can introduce a delay. This gives us a buffer after making changes before Nodemon restarts the app, helping stabilize our development environment.

    For adding a one-second delay, you’d configure it like this:

    {
    "delay": 1000
    }

These tweaks have made a noticeable difference in managing the development flow, especially in complex projects. I think implementing these could really streamline how we handle automatic restarts in our development process!

Conclusion

As we wrap up discussing Nodemon, I wanted to share some common issues you might run into and how to troubleshoot them. Even though Nodemon simplifies our development by auto-restarting our apps, sometimes it might not work as expected.

Here are a few things to check if you encounter problems:

  1. Monitoring Files: If Nodemon isn't restarting when you change files, double-check your configuration to ensure it’s watching the right files and directories. Sometimes, if we ignore too many files, Nodemon might miss changes in crucial areas.

  2. Crashes or Non-starts: Make sure there are no errors in your Node.js app and that all environment variables are set correctly. These can often stop Nodemon from running properly.

  3. Permissions on Windows: If you’re on Windows and facing permission issues, try running Nodemon in an administrator command prompt, which can resolve these problems.

  4. Software Updates: Lastly, keeping Nodemon and Node.js updated to their latest versions can solve a lot of known issues.