Skip to content

How to Use Nodemon with NodeJS Apps

In Node.js, whenever you make any changes in your application source code, you would have to manually restart the application process for those changes to take effect. You can eliminate this extra step by using Nodemon to restart the process automatically.

What is Nodemon?

So, what is Nodemon? Nodemon is a command-line interface (CLI) utility. That will watch the file system for any file changes in the directory you ran Nodemon in. And automatically restart your application process either is a hanging process (such as a web server process) or a one-time run application that cleanly exits. Which will speeds up the development.

Just use “nodemon” instead of “node” to run your code, and your process will automatically restart when your code changes. Nodemon doesn’t require any changes to your existing application. By default, it supports Node.js & CoffeeScript, but it can run any executable (such as python scripts).

Install Nodemon

I assume you have Node.js and NPM installed. You can install Nodemon on your machine, globally, or locally on your project using NPM.

Install Nodemon globally

To install it globally, run "npm install --global nodemon". And Nodemon will be installed on your system path, and you will be able to use the "nodemon" command directly on the command line.

Install Nodemon locally

You can also install Nodemon locally as a development dependency by running "npm install --save-dev nodemon". With a local installation, Nodemon will not be available in your system path. And this means if you try running the "nodemon" command, you will receive “command not found”. Instead, the local installation of Nodemon can run by calling it from within an NPM script.

Use Nodemon with Node.js

We can use Nodemon to start a Node.js script. For example, here, we have a simple Express server in the index.js file.

We want to run it and watch for changes. So we will say "./node_modules/.bin/nodemon ./src/index.js". Now, if I make a small change in the code and hit save. Nodemon will restart the application, and we can see the changes by hitting the refresh button.

You can pass in arguments the same way as if you were running the script with Node.js. Here I’m passing “port” as an argument to my application. "./node_modules/.bin/nodemon ./src/index.js 4000".

You can also pass the “inspect” flag to Node.js to run it with a debugger attached through the command line as you would normally do "./node_modules/.bin/nodemon --inspect ./src/index.js".

Suppose you have a package.json file for your application. In that case, you can omit the main script entirely, and Nodemon will read the package.json for the “main” property and use its value instead. Nodemon will also search for the “start” script in package.json if there is no “main” property in the package.json file.

Suppose you need to restart your application manually instead of stop and restart Nodemon. In that case, you can type “rs” with a carriage return, and Nodemon will restart your application.

Configure Nodemon

We can configure Nodemon in various ways. By default, Nodemon will look for a JSON file called "nodemon.json" in the current working directory. Alternatively, you can pass the config file path by using the –config flag so that you could do something like "./node_modules/.bin/nodemon --config", and then you could say "my.nodemon.json". And this will look for this file instead.

Let’s go over a few of the main options:

“watch” is a list of specific files, file patterns, or directories we want to trigger a program restart when they changed.

On the other hand, “ignore” is a list of specific files, file patterns, or directories that we don’t want to trigger a program restart when they changed.

“ext” is a comma-separated list of the file extensions Nodemon monitors.

“env” is a list of environment variables we want to pass to our application.

And “execMap” is a mapping between a file type or extension to a binary to execute the file with it. For example, we can tell Nodemon to run “js” files using “node”.

GitHub Repo

Here is the Github repository link where you can find the full source code of this article, clone the project and run it on your machine.