How to launch your Node.js applications automatically with nodemon

0 Shares
0
0
0
0

Introduction

In Node.js, you need to restart the process for the changes to take effect. This adds an extra step to your workflow. You can eliminate this extra step by using nodemon to automatically restart the process.

nodemon is a command-line interface (CLI) tool developed by rem that packages your Node application, watches the file system, and automatically restarts the process. In this article, you will learn how to install, set up, and configure nodemon.

Prerequisites
  • Node.js installed

Step 1 – Install nodemon

First you need to install nodemon on your machine. You can use the tool globally or locally on your project using npm Or yarn Install.

You can start nodemon globally with npm Install:

npm install nodemon --global

Or with Yarn:

yarn global add nodemon

You can also install nodemon locally. When performing a local installation, you can install nodemon as a developer dependency with --save-dev (or --dev).

Install Nodemon locally with npm Install:

npm install nodemon --save-dev

Or with Yarn:

yarn add nodemon --dev

One thing to note about a local installation is that you cannot use the nodemon command directly:

Outputcommand not found: nodemon

You can run the locally installed package:

./node_modules/.bin/nodemon.js [your node app]

You can also use it in npm scripts or with npx.

Step 2 – Setting up an Example Express Project with nodemon

You can use nodemon to start a Node script. For example, if you have an Express server in the file server.js Launch, you can nodemon Launch and see changes like this:

nodemon server.js

You can pass arguments the same way you would run a script with Node:

nodemon server.js 3006

The process starts over every time you make a change to a file with one of the default extensions observed (js, .mjs, .json, .coffee, or .litcoffee) in the current directory or a subdirectory.

Let's write a sample server.js file that sends the message out: Dolphin application listening on port ${port}!.

const express = require('express')
const app = express()
const port = 3000
app.listen(port, ()=> console.log(`Dolphin app listening on port ${port}!`))

Run the example with nodemon:

nodemon server.js

The terminal output displays the following output:

Output
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Dolphin app listening on port 3000!

While nodemon is still running, let's make a change to the server.js file. Change the output to another message: Shark application listening on port ${port}!.

The terminal output displays the following output:

Output
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Shark app listening on port 3000!

The terminal output from the Node.js application is showing the new changes.

You can type at any time rs and hitting ENTERRestart the process.

On the other hand, nodemon will also look for the main file specified in your project's package.json file:

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

If the main file is not specified, nodemon looks for a startup script:

{
// ...
"scripts": {
"start": "node server.js"
},
// ...
}

Once you have made the changes to package.json, you can then call nodemon to launch the sample application in watch mode without having to go through server.js.

Step 3 – nodemon options

You can change the existing configuration settings for nodemon.

Let's look at some of the main options:

  • –exec: Use the –exec switch to specify a binary to execute the file. For example, when combined with the ts-node binary, –exec can be useful for watching changes and executing TypeScript files.
  • –ext: Specify different file extensions to watch. For this switch, provide a comma-separated list of file extensions (for example, –ext js,ts).
  • –delay: By default, nodemon waits one second before restarting the process when a file changes, but with the –delay switch, you can specify a different delay. For example, nodemon –delay 3.2 for a 3.2 second delay.
  • –watch: Use the –watch switch to specify multiple directories or files to watch. Add a –watch switch for each directory you want to watch. By default, the current directory and its subdirectories are watched, so with -watch you can limit it to specific subdirectories or files.
  • –ignore: Use the –ignore switch to ignore certain files, file patterns, or directories.
  • –verbose: A more verbose output with information about what file(s) changed to restart the startup.

You can see all available options with the following command:

nodemon --help

Using these options, let's create a command to satisfy the following scenario:

  • Watching the server directory
  • Specify files with the .ts extension
  • Ignore files with the extension .test.ts
  • Running the file (server/server.ts) with ts-node
  • Wait three seconds to reboot after changing file
nodemon --watch server --ext ts --exec ts-node --ignore '*.test.ts' --delay 3 server/server.ts

The terminal output displays the following output:

Output
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): server
[nodemon] watching extensions: ts
[nodemon] starting `ts-node server/server.ts`

This command combines the –watch, –ext, –exec, –ignore, and –delay options to meet the requirements of our scenario.

Step 4 – Configure nodemon

In the previous example, adding configuration switches when running nodemon can be tedious. A better solution for projects that require complex settings is to define these options in a nodemon.json file.

For example, here are the same configurations as the previous command line example, but placed in a nodemon.json file:

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

Note the use of execMap instead of the –exec switch. execMap allows you to specify binaries for specific file extensions.

Alternatively, if you prefer not to add a nodemon.json configuration file to your project, you can add these settings to the package.json file under a nodemonConfig key:

{
"name": "nodemon-example",
"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 desired script:

nodemon server/server.ts

nodemon picks up the settings and uses them. This way, your settings can be saved, shared, and replicated to avoid copy-pasting or typing errors on the command line.

Result

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like