Taming Nodemon: Preventing Unnecessary Node.js Restarts from Logs and Build Artifacts
Stop Nodemon from restarting your Node.js server on irrelevant file changes like logs or build artifacts. Use --watch, --ext, and --ignore to precisely scope file watching for faster, cleaner development feedback.
09 Jun 2026, 22:17 UTC

Stop Nodemon from Restarting on Irrelevant File Changes
The common frustration with development tooling is wasted CPU cycles: Nodemon restarts your Node.js server every time a log file is written, a build output (like a dist/ folder) is updated, or a dependency file in node_modules changes. The useful takeaway is that you can precisely scope Nodemon's file watching using --watch, --ext, and --ignore to ensure restarts only happen when source code changes.
Scoping the Watch: Directories and Extensions
By default, Nodemon watches many paths. You must explicitly tell it what to look at. Use the --watch flag to limit directory scanning (e.g., --watch src --watch config) and --ext to restrict file types (e.g., --ext js,ts,graphql).
Excluding Noise with Ignore Patterns
The --ignore flag is crucial for excluding generated or non-source files. For instance, --ignore logs/* dist/* .git/* prevents restarts from artifacts.
Configuration Consistency via nodemon.json
For team consistency, define these scopes in nodemon.json rather than relying solely on long command-line flags. The configuration object supports "watch", "ext", and "ignore" arrays.
Worked Example: Selective Watching
To watch only TypeScript and JavaScript files within the src directory, ignore the logs directory, and execute node src/index.js, use this command in your project root:
npx nodemon --watch src --ext ts,js --ignore logs/ --exec "node src/index.js"
Verification: Touch a file inside logs/ and observe that the Node.js process does not restart. Conversely, modify src/app.ts and confirm a restart occurs.
Trade-off: Watch Mode Reliability
On networked file systems or within certain Docker volumes, native file event watching can be unreliable or delayed. If restarts are missed, use --legacy-watch. However, be aware that polling modes consume more CPU than event-based watching and should only be used when event reliability fails.
Limitation: CLI flags always override settings in nodemon.json. Always check both if behavior is unexpected.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.