Are you tired of messy CLI flags every time you run Nodemon? Want a clean, manageable, and powerful setup that saves you time and keeps your development workflow smooth? You’re in the right place.
In this article, I’ll walk you through mastering Nodemon configuration using nodemon.json
, the nodemonConfig
section in package.json
, and command-line flags. You’ll learn how to watch specific files, ignore unnecessary folders, run custom commands, set environment variables, and debug your config—all in a way that’s easy to understand and practical to implement.
If you haven’t installed Nodemon yet, don’t worry! I’ve written a step-by-step guide on how to install Nodemon in Node.js — check it out if you need to get set up. Ready? Let’s dive into Nodemon config like a pro.
Nodemon Config with nodemon.json
By default, Nodemon looks for a nodemon.json
file in the root folder of your project. This JSON file holds your configuration settings and keeps things neat and centralized.
If you want different setups—for example, one for development and one for production—you can use the --config
flag to point Nodemon to another config file.
npx nodemon --config nodemon.dev.json # For Development
npx nodemon --config nodemon.prod.json # For Production
This allows switching between development and production environments.
watch
: What Should Nodemon Monitor?
The watch
key tells Nodemon which folders or files to monitor for changes. Without it, Nodemon watches everything by default, which can cause unnecessary restarts.
{
"watch": ["src"]
}
ignore
: What to Skip
You might want to ignore folders like logs
that change often but don’t require a restart.
{
"watch": ["src"],
"ignore": ["src/logs"]
}
Keep in mind: the ignore
key prevents restarts when files change in those folders but Nodemon still monitors them. To fully exclude a folder, ensure it’s not in the watch
array at all.
Watch & Ignore Patterns
You can use these patterns with watch
and ignore
rules:
"src/"
watches the wholesrc
folder."src/*"
watches all files directly in thesrc
folder."src/*.js"
watches only.js
files insrc
folder."src/**/*.js"
recursively watches all.js
files insrc
folder and its subfolders.
Be careful— ignore patterns must be absolute paths. Relative ones can behave unexpectedly.
Both watch
and ignore
are arrays. You can add multiple folders to watch or ignore. For example, you can add the config
folder to the watch list and ignore the tmp
folder inside src
:
{
"watch": ["src", "config"],
"ignore": ["src/logs", "src/temp"]
}
ext
: Extensions to Watch
The ext
key instructs Nodemon to only restart the process when files with specific extensions change. It is useful for projects with mixed file types.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json"
}
exec
Key: Run Custom Commands
It defines what command to run when a restart is triggered. You can change the default script Nodemon runs and also the binary it uses to run that script, it can be node
, babel-node
, ts-node
or anything else.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json",
"exec": "node src/server.js"
}
execMap
: Map File Extensions to Runtimes
It tells Nodemon which binary to use for specific file types. For JavaScript files, it defaults to node
. This is especially useful when working with ts-node
or babel-node
.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json",
"exec": "node src/server.js",
"execMap": {
"js": "node"
}
}
env
Key: Set Environment Variables
The env
key sets environment variables after Nodemon starts.
This won’t affect any logic that runs before Nodemon starts. Use your npm script to set NODE_ENV
before execution if needed.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json",
"exec": "node src/server.js",
"execMap": {
"js": "node"
},
"env": {
"PORT": "3000",
}
}
delay
Key: Prevent Multiple Restarts
This helps avoid multiple back-to-back restarts when many files change at once.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json",
"exec": "node src/server.js",
"execMap": {
"js": "node"
},
"env": {
"PORT": "3000",
},
"delay": "500ms"
}
verbose
: Debug Your Config
The verbose key shows detailed logs so you can see what Nodemon is doing under the hood, what’s being watched, ignored, and executed. This makes it easier to troubleshoot your Nodemon config directly from the terminal.
{
"watch": ["src"],
"ignore": ["src/logs"],
"ext": "js,json",
"exec": "node src/server.js",
"execMap": {
"js": "node"
},
"env": {
"PORT": "3000",
},
"delay": "500ms",
"verbose": true
}
This gives you full control over your Nodemon configuration without relying solely on messy CLI flags. However, CLI flags remain useful for quick overrides and one-time settings!
Move Config to package.json
Instead of using nodemon.json
, you can add a nodemonConfig
section to package.json
. Moving the config to package.json
is as easy as pasting nodemon.json
content under the nodemonConfig
key in package.json
.
{
"nodemonConfig": {
"watch": ["src"],
"ignore": ["src/logs"]
}
}
It is handy for smaller projects and you don’t need an extra file.
Nodemon CLI Options and Flags
Nodemon’s CLI options override file-based configs. Let’s run an example:
nodemon --watch src --ignore src/logs --ext js,json --exec "node src/server.js" --delay 500ms --verbose
This CLI command covers most key configurations from nodemon.json
, but certain options like env
and execMap
require additional setup.
--watch
is equivalent to the watch
key in nodemon.json
. It defines which folder to monitor and you can also use its shorthand version which is -w
, so nodemon --watch
is equal to nodemon -w
.
--ignore
does the same as ignore key. Just use multiple --ignore
flags to ignore multiple folders.
nodemon --watch src --ignore src/logs --ignore src/tmp
--ext
, --exec
, and --delay
work the same way as the ext
, exec
, and delay
keys in nodemon.json
. The shorthand version of --exec
is -x
, so nodemon --exec
is the same as nodemon -x
.
--verbose
turns debug output on.
Here’s a quick summary of the most common Nodemon CLI options and their shorthand versions:
Option | Shorthand | Purpose |
--watch | -w | What folders to watch |
--ignore | -i | What to skip |
--ext | -e | Extensions to watch |
--exec | -x | Command to run |
--delay | Restart delay | |
--verbose | Debug output |
GitHub Repo
Want to test this out quickly? We’ve created a sample repo with everything in place.
Clone the repo, install dependencies, and try running Nodemon on your machine right away.
Final Thoughts
Nodemon is an incredibly useful tool for improving your Node.js development workflow, but its true power shines when you configure it properly. With nodemon.json
, package.json
, and CLI flags at your disposal, you can fine-tune how and when your app restarts, reduce noise from irrelevant file changes, and even inject custom logic or variables into the runtime.
Did this guide help you tidy up your Nodemon setup? Leave a comment or watch our full Nodemon config video tutorial for a hands-on walkthrough!