Nodemon is a command-line interface utility that assists in building Node.js apps by dynamically restarting the node application when it identifies file changes in the directory.

This article will teach you how to install and configure nodemon to suit your particular needs, as well as explore a common error that occurs when using nodemon

Why You Should Use Nodemon

When developing web servers or backend apps, you’re bound to change your code to handle errors and improve the structure and logic of your program.

Nodemon assists by automatically restarting a server when you make changes to its code. This helps in saving time and allows for easier debugging.

How to Install Nodemon

You can install Nodemon either locally as a development dependency or globally on the system path.

To install nodemon globally, run the following npm command in your computer's terminal:

        npm install nodemon --globally  

To install nodemon locally, you should run the following npm command inside your project directory in your terminal:

        npm install nodemon --save-dev

Starting Nodemon in Your Web Server Application

Starting nodemon and getting it to watch your web server application involves a few steps. The code block below is a basic script for a server that logs a string message to the console:

        // app.js
const express = require('express');
const app = express();

app.listen((5000), ()=>{
    console.log(`I'm learning about nodemon`)
});

To start watching this script, run the nodemon command followed by the name of the file it should watch:

        nodemon app.js
    

The resulting output should include something like the following text:

        [nodemon] starting `node app.js`
I'm learning about nodemon

Once nodemon has successfully started, any changes to the app.js file will cause a reload to the entire server app.

You can exit nodemon by hitting Ctrl + C in your computer's terminal. You can also restart the nodemon process manually by entering the rs command.

Using Nodemon With Command-Line Options

Nodemon has several command-line options that you can use to modify its behavior.

To see a list of all the available nodemon options and their functions, run this command:

        nodemon --help options

Among the many options available are;

  • --delay: When a file changes, nodemon waits for a second by default before restarting the process. You can specify a different delay with the --delay switch. You can select the amount of time that nodemon will wait before restarting. For example:
            nodemon --delay five app.js
        
  • --ignore: The ignore switch option allows you to ignore specific files in your web server application. You may use the switch option like so:
            nodemon --ignore lib/app.js
  • --watch: By default, when running, nodemon monitors the current working directory. To take control of that option, use the --watch option to add specific file paths and change what directory is being watched. For example, monitoring a server directory:
            nodemon --watch server
  • --exec: Although it’s a JavaScript utility, you can also use Nodemon to watch non-JavaScript files like TypeScript, Python, and Golang. You can use the --exec option to get nodemon’s automatic reloading function in such scripts. For example, using nodemon in a TypeScript script:
            nodemon --exec ts-node
  • --ext: Nodemon, by default, searches for files with JavaScript, TypeScript, CoffeeScript, and JSON extensions. This is because Node.js projects can work with TypeScript and CoffeeScript files that compile into pure JavaScript before being executed. On the other hand, JSON files are also monitored because they are often used to store metadata for Node.js projects. You can use the -e (or --ext) switch to specify a comma-separated list of files to watch. For example, to monitor files with extensions .js, .json, and .mjs:
            nodemon --ext js,json,mjs

Using Nodemon Configuration Files

Nodemon supports local and global configuration files. You can find these in your home directory or the project’s root directory; they’re typically called nodemon.json files. This way, you can save your nodemon configurations and easily reuse them with other projects.

Command line options will always override the configuration file settings. The working priority order includes command-line options, local, and configuration files.

A configuration file can use any command line option as a JSON key value.

For example:

        {
  "watch": [ "server" ],
  "ext": [ "js", "ts", "json" ],
  "delay": "5000",
  "ignore": [ "lib/app.js" ]
}

In the above nodemon.json file, nodemon is set to watch the server directory, specify files with .js, .ts, and .json extensions, delay for 5 seconds before restarting after file changes, and finally ignore file changes in lib/app.js.

As an alternative, nodemon supports using package.json for configuration if you keep all package configurations in one location. Under nodemonConfig in the package.json file, specify the configuration in the same format as you would for a configuration file.

For example, this package.json file includes the same configuration as the nodemon.json file above:

        {
  "name": "nodemon",
  "version": "",
  "description": "",
  "nodemonConfig": {
    "watch": [
      "server"
    ],
    "ext": [ "js", "ts", "json" ],
    "delay": "5",
    "ignore": [
      "lib/app.js"
    ]
  }
}

Handling Nodemon Errors

While you’re developing your app, you’ll need to handle errors in JavaScript along the way. To handle these errors, you will first need to know why they happen.

Here’s a common error that you can come across when working with nodemon:

        [nodemon] app crashed - waiting for file changes before starting...

There are several reasons why this error message could occur and several ways to fix the causes.

  • Wrong JavaScript syntax in the files nodemon is watching. To prevent this error, go through your JavaScript code to identify any syntax errors within it.
  • Running multiple tasks in the background can interfere with this particular task. Try stopping all background processes and running the program again to fix this.
  • Incorrect directory organization can also cause problems. For example, not having the app.js and package.json files in the same directory. Ensure you structure your directory properly so that it won't affect the running of your programs.

Getting Comfortable With Nodemon

You’ve seen the usefulness of nodemon and how it can create a better workflow, reduce errors, and make debugging faster and easier.

However, errors can still occur when working with nodemon. As a developer, learning how to troubleshoot these errors is important.