Restart Node.js Apps Automatically with nodemon

Tags: Node.js nodemon

Restart your Node.js Apps Automatically with nodemon

Presentation

In Node.js, you have to restart the cycle to make changes produce results. This adds an additional progression to your work process to have the progressions happen. You can kill this additional progression by utilizing nodemon to restart the cycle naturally.

nodemon is an command line interface (CLI) utility created by @rem that wraps your Node application, watches the document framework, and naturally restarts the cycle.

In this article, you will find out about introducing, setting up, and arranging nodemon.

Essentials

In the event that you might want to track with this article, you will require:

Node.js introduced locally, which you can do by following How to Install Node.js and Create a Local Development Environment.

Stage 1 — Introducing nodemon

To start with, you should introduce nodemon on your machine. Introduce the utility either universally or locally on your undertaking utilizing npm or Yarn:

Global Installation

You can introduce nodemon internationally with npm:

npm install nodemon -g

Or with Yarn:

yarn global add nodemon

Local Installation

You can also use npm to install nodemon locally. When performing a local installation, we can use --save-dev (or --dev) to install nodemon as a dev dependency:

npm install nodemon --save-dev

Or with Yarn:

yarn add nodemon --dev

One thing to note about local installation is that you will not be able to use the nodemon command directly from the command line:

command not found: nodemon

However, you can use it as part of some npm scripts or with npx. This concludes the nodemon installation process. Next, we will use nodemon in the project.

Stage 2 - Set up a sample Express project with nodemon

We can start Node scripts using nodemon. For example, if we have an Express server setting in the index.js file, we can start it and notice the following changes:

nodemon index.js

You can pass parameters in the same way as running scripts with Node:

nodemon index.js 5000

The process will restart every time a file with one of the default monitoring extensions (.js, .html, .xml, .car or .whitecat) in the current directory or subdirectory is changed.

Suppose we have written a sample index.js file that outputs the following message:

Sirnode app listening on port ${port}!

We can run the example using nodemon:

nodemon index.js

We see this output:

[nodemon] 1.17.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting  `node index.js`
Sirnode app listening on port 5000!

While nodemon is still running, let's make changes to the index.js file to output the following message: NodeSir application is listening on ${port}! port.

We see the subsequent further terminal output:

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
NodeSir app listening on port 5000!

The terminal output of the Node.js application will be displayed as expected. You can restart the process at any time by typing rs and pressing Enter. In addition, nodemon will also look for the specified main file in the project's package.json file:

package.json
{
  // ...
  "main": "server.js",
  // ...
}

Or, a start script:

package.json
{
  "scripts": {
    "start": "node server.js"
  },
}

After making changes to package.json, you can call nodemon to start the sample application in monitoring mode without passing in index.js.

Stage 3 — Using Options

You can modify the configuration settings available for nodemon.

Let's look at a few main options:

--exec: Use the --exec switch to specify the binary file to execute the file. For example, when used in conjunction with ts-node binary files, --exec can be used to monitor changes and run TypeScript files.

--ext: Specify the extension of other files to watch. For this switch, please provide a comma-separated list of file extensions (for example --ext js, ts).

--delay: By default, when the file changes, nodemon waits one second to restart the process, but using the --delay switch, you can specify other delays. For example, nodemon --delay 3.2 means a delay of 3.2 seconds.

--watch: Use the --watch switch to specify multiple directories or files to be monitored. Add a --watch switch for each directory you want to watch. By default, the current directory and its subdirectories will be monitored, so use --watch to narrow it down to specific subdirectories or files.

--ignore: Use the --ignore switch to ignore certain files, file patterns or directories.

--verbose: More detailed output, which contains information about which files were changed to trigger a restart.

You can see all available commands with the following command:

nodemon --help

Stage 4 — Using Configurations

Inside the previous example, including configuration switches when working nodemon can get pretty tedious. A better answer for initiatives that want unique configurations is to specify those configs in a nodemon.json file.

For example, this is the same configuration as the previous command line example, but placed in the nodemon.json file:

nodemon.json
{
  "watch": ["server"],
  "ext": "ts",
  "ignore": ["*.test.ts"],
  "delay": "3",
  "execMap": {
    "ts": "ts-node"
  }
}

Please note the use of execMap instead of the --exec switch. execMap allows you to specify which binary file should be used under certain file extensions.

In addition, if you don't want to add the nodemon.json configuration file to your project, you can add these configurations to the package.json file under the nodemonConfig key:

nodemon.json
{
  "name": "test-nodemon",
  "version": "1.0.0",
  "description": "",
  "nodemonConfig": {
    "watch": [
      "server"
    ],
    "ext": "ts",
    "ignore": [
      "*.test.ts"
    ],
    "delay": "3",
    "execMap": {
      "ts": "ts-node"
    }
  },

After making changes to nodemon.json or package.json, you can start nodemon with the required script:

nodemon server/server.ts

nodemon will select and use the configuration. In this way, you can save, share and repeat your configuration to avoid copying and pasting or typing errors in the command line.

Summary

In this article, you explored how to use nodemon in Node.js applications. This tool helps to automate the process of stopping and starting the node server to see changes.

For more information on available features and troubleshooting errors, please consult the official documentation.

If you want to learn more about Node.js, please check out our Node.js theme page for exercises and programming projects.