patternjavascriptTip
Node.js --watch flag: built-in file watching without nodemon
Viewed 0 times
Node.js 18.11+
node --watchnodemon alternativebuilt-in watchfile watching nodedev server restart
node
Problem
Teams install nodemon as a dev dependency to restart a Node server on file changes, adding an extra tool and configuration to maintain.
Solution
Node.js 18.11+ ships a built-in --watch flag that restarts the process when any required file changes. No extra packages needed.
# package.json
{
"scripts": {
"dev": "node --watch src/index.js",
"dev:ts": "node --watch --experimental-strip-types src/index.ts"
}
}
# Watch specific extensions only
node --watch --watch-path=src src/index.js
# For TypeScript without compilation (Node 22.6+ experimental)
node --experimental-strip-types --watch src/index.ts
# package.json
{
"scripts": {
"dev": "node --watch src/index.js",
"dev:ts": "node --watch --experimental-strip-types src/index.ts"
}
}
# Watch specific extensions only
node --watch --watch-path=src src/index.js
# For TypeScript without compilation (Node 22.6+ experimental)
node --experimental-strip-types --watch src/index.ts
Why
Node's --watch uses native filesystem events (inotify/kqueue/FSEvents) through libuv. It monitors all files loaded via require/import and restarts the process when any of them change. This is the same mechanism nodemon uses but with zero configuration.
Gotchas
- --watch is not meant for production — it restarts the entire process on any change
- Use --watch-path to add directories not auto-detected by Node's import graph (e.g. config files)
- --experimental-strip-types (Node 22.6+) runs TypeScript without tsc — types are stripped, not checked
- nodemon still offers more configuration options (ext, ignore, delay) for complex cases
Context
Local development of a Node.js server or script without wanting to install nodemon
Revisions (0)
No revisions yet.