nodemon Restart on file change

Version 3.1.14 · source

Start here

nodemon <script.js>
Run and restart on change
nodemon
Use package.json main/start
nodemon -w <dir> <script.js>
Watch only one path
nodemon -e js,json <script.js>
Pick watched extensions
nodemon --exec "<cmd>"
Run any command
nodemon -d 2 <script.js>
Wait 2 s before restart
rs
Type + Enter: manual restart
nodemon.json
Save options per project

Concepts

  1. Start run command
  2. Watch files by ext/path
  3. Debounce --delay
  4. Restart same command

Running

nodemon <script.js>
Run script, restart on changes
nodemon
Run main or scripts.start of package.json
nodemon <script.js> <arg1> <arg2>
Pass arguments to the script
nodemon <script.js> -- --port 3000
Stop nodemon parsing; pass flags on
Everything after -- goes to your script, even if it looks like a nodemon flag.
nodemon --inspect <script.js>
Restart with the Node debugger
nodemon --exec "<cmd>"
Run any command instead of node
nodemon -x python <app.py>
Run a Python script
nodemon -x "npx ts-node" <app.ts>
Run TypeScript through ts-node
nodemon -C <script.js>
Start only after the first change
nodemon --cwd <dir> <script.js>
Run from another directory
nodemon --spawn <script.js>
Use spawn instead of fork (Node only)
nodemon -I <script.js>
Hand stdin straight to the app
Disables typing rs; use it when the app reads from stdin itself.
nodemon --exitcrash <script.js>
Exit if the app crashes

Watching

nodemon -w <dir> <script.js>
Watch a directory or file
nodemon -w <a> -w <b> <script.js>
Watch several paths
nodemon -e js,json,html <script.js>
Watch these extensions
nodemon -e "ts tsx" <script.js>
Extensions separated by spaces
nodemon -i <path>
Ignore a file or directory
nodemon -i "*.test.js" <script.js>
Ignore a glob (quote it)
nodemon -i "docs/**" <script.js>
Ignore a whole tree
nodemon -d <seconds> <script.js>
Debounce restarts
Plain numbers are seconds on the CLI, but milliseconds in nodemon.json.
nodemon -L <script.js>
Poll for changes (Docker, network drives)
Last resort: polling uses CPU. Needed when file events do not reach the watcher.
nodemon -L -P <ms> <script.js>
Set the polling interval

Control

rs
Restart manually (type, then Enter)
Ctrl+C
Quit nodemon and the app
nodemon --signal SIGTERM <script.js>
Kill signal used for restarts
nodemon -V <script.js>
Show which file caused a restart
nodemon -q <script.js>
Only print start and stop messages
nodemon --dump
Print the effective configuration
nodemon --no-colors
Plain output
nodemon --no-update-notifier
Skip the update check
nodemon -v
Show the version
nodemon --help options
List all options
nodemon --help config
Explain config files

Configuration

nodemon.json
Per-project config next to the code
nodemonConfig in package.json
Same keys, inside package.json
Ignored when a local nodemon.json or --config file exists.
nodemon --config <file>
Use another config file
$HOME/nodemon.json
Global defaults for all projects
Common config keys
Key Value Matches
watch array of paths -w
ext "js,json" -e
ignore array of globs -i
exec command string -x
delay milliseconds -d
verbose true / false -V
legacyWatch true / false -L
env object of variables (config only)
execMap extension to runner (config only)
events event to shell command (config only)

Recipes

Project nodemon.json
{  "watch": ["src"],  "ext": "ts,json",  "ignore": ["src/**/*.spec.ts"],  "exec": "ts-node src/index.ts",  "delay": 500,  "env": { "NODE_ENV": "development" }}
Run a different language
nodemon --exec python3 app.py# or map extensions in nodemon.json{ "execMap": { "py": "python3", "rb": "ruby" } }
Run a build or test on change
nodemon -w src -e js --exec "npm test"nodemon -w src -e ts --exec "npm run build"
Hook into restarts
{  "events": {    "restart": "echo restarted"  }}