Using Nodemon and Watch in Node.js for Live Restarts
Node.js development can be slower than necessary if you have to stop and restart your new application every time you make a change. This tutorial provides two solutions to improve your coding workflow.
If you’ve ever developed a PHP application, you’ll know you can make updates and refresh your browser to test the changes. A web server such as Apache or NGINX receives a your request for a PHP file, then passes the content to a PHP interpreter which executes the code. The server returns resulting output (typically HTML or JSON) to the calling browser. In other words, the code runs dynamically on every request.
Node.js takes a different approach for web apps: your JavaScript application is a web server. Running node index.js
initializes the app, loads all modules, and launches a server which can respond to incoming requests. Changing a file makes no difference to the app’s output because it’s already running in memory. To test updates, you must shut it down with Ctrl | Cmd + C and run node index.js
again.
Node’s stop and restart process becomes frustrating when you’re making lots of changes during debugging or those rare times of undisturbed productivity. Fortunately, there are two solutions:
nodemon
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 Configuration
nodemon
has its own set of command-line arguments which take priority over configuration elsewhere. You can also define configuration in:
- a
"nodemonConfig"
section in your project’spackage.json
file - a local
nodemon.json
configuration file in the project directory, and/or - a global
nodemon.json
configuration file used when runningnodemon --config <file>
from the command line
The following parameters/settings can are commonly used.
watch
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"
]
}
ignore
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"
]
}
ext
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"
}
legacyWatch
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
}
delay
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
}
verbose
Shows verbose output logs:
nodemon --verbose ./index.js
Or in a nodemon.json
configuration file:
{
"verbose": true
}
env
Sets specific environment variables a nodemon.json
configuration file:
{
"env": {
"NODE_ENV": "development",
"SERVER_PORT": 8000
}
}
Other executables
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"
}
}
Advanced nodemon
nodemon provides more advanced functionalit,y should you require it:
- send a signal so you can gracefully handle a shutdown
- trigger events when nodemon’s state changes
- pipe output to other processes
- load nodemon as a module into your project
- spawn nodemon as a child process, and
- use nodemon in Gulp and Grunt workflows.
Node.js --watch
Mode
nodemon 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.
Summary
Auto restarting your Node.js app is something you’ll find increasingly useful as your experience increases. Consider it as part of your development workflow in all your projects.