Learn how to use nodemon and watch to automatically restart your Node.js server whenever you make changes. A must-read for Node.js developers.
nodemon is a third-party Node.js module developed by JavaScript guru Remy Sharp. (He says you can pronounce it as you choose!)
You can install nodemon
as a global module:
npm install -g nodemon
Then replace node
with nodemon
in development start-up commands. For example, consider this command:
node --inspect index.js arg1 arg2
The command above will now look like this:
nodemon --inspect index.js arg1 arg2
Your application starts as normal, but it will automatically restart when you edit and save a source file. There’s no need to press Ctrl | Cmd + C and run again, although you can type rs
and press Enter to force a restart.
Note: nodemon is a server-side solution and doesn’t refresh any browser tabs you have pointed at your app. You can implement live reloading with tools such as Browsersync or esbuild.
For nodemon help, enter:
nodemon --help
nodemon
has its own set of command-line arguments which take priority over configuration elsewhere. You can also define configuration in:
"nodemonConfig"
section in your project’s package.json
filenodemon.json
configuration file in the project directory, and/ornodemon.json
configuration file used when running nodemon --config <file>
from the command lineThe following parameters/settings can are commonly used.
nodemon monitors JavaScript files in the current working directory, but you can explicitly set specific paths with wildcards on the command line:
nodemon --watch lib1 config/*.json ./index.js
Or you can do this in a nodemon.json
configuration file:
{
"watch": [
"lib1",
"config/*.json"
]
}
Similarly, you can choose to ignore paths:
nodemon --ignore lib2 config/build.json ./index.js
Or you can do this in a nodemon.json
configuration file:
{
"ignore": [
"lib2",
"config/build.json"
]
}
You can monitor specific files by their extension. For example, you can monitor js
, cjs
, mjs
, json
, and njk
template files like so:
nodemon --ext "js,cjs,mjs,json,njk" ./index.js
Or you can do so in a nodemon.json
configuration file:
{
"ext": "js,cjs,mjs,json,njk"
}
File watching can fail in some environments, such as Docker containers reading files from a mounted drive. Switching to legacy watch mode uses polling to check whether files have changed. From the command line:
nodemon --legacy-watch ./index.js
Or in a nodemon.json
configuration file:
{
"legacyWatch": true
}
nodemon waits one second before triggering a restart. This can be useful when you’re typically saving many files at once. You can change the delay from the command line — for example, to five seconds:
nodemon --delay 5 ./index.js
Or in a nodemon.json
configuration file (note this uses milliseconds rather than seconds):
{
"delay": 5000
}
Shows verbose output logs:
nodemon --verbose ./index.js
Or in a nodemon.json
configuration file:
{
"verbose": true
}
Sets specific environment variables a nodemon.json
configuration file:
{
"env": {
"NODE_ENV": "development",
"SERVER_PORT": 8000
}
}
Finally, you can launch applications written in other languages using nodemon. For example, to start a perl
script which restarts automatically:
nodemon --exec "perl" ./app.pl
You can also define lists of executables in a nodemon.json
configuration file with their extension name:
{
"execMap": {
"pl": "perl"
}
}
nodemon provides more advanced functionalit,y should you require it:
--watch
Modenodemon remains the tool of choice if you have sophisticated application start-up requirements. However, if you’re using Node.js 18.11 (released late 2022) or newer, it provides an experimental --watch
option to restart your app without having to install a nodemon or any other third party module. For example, to the start command:
node --inspect index.js
This becomes:
node --inspect --watch index.js
Node.js restarts when any imported file changes. There are no other control options, so if it’s not suitable for your project, consider using nodemon instead