Fine‑Tune Nodemon: Watch Only Your src Folder and Skip node_modules & .git
Configure Nodemon to watch only your src folder while ignoring node_modules and .git. Follow a step‑by‑step guide that includes a nodemon.json example, verification checks, and recovery tips for a smoother dev workflow.
29 Jan 2026, 17:47 UTC

Desired Outcome
When developing a Node.js application, you often want Nodemon to restart the app only when you change source files. Watching the entire project tree—including node_modules and .git—can slow restarts and generate noisy logs. This guide shows how to configure Nodemon to watch just the src directory while ignoring the other two, using a nodemon.json file.
Prerequisites
- Node.js 12+ installed (runtime and
npmoryarn) - Project with a
srcfolder and an entry point (e.g.,src/index.js) - Local Nodemon installation:
npm install --save-dev nodemon
Focused Procedure
- Create
nodemon.jsonin the project root. This file tells Nodemon how to behave.{ "watch": ["src"], "ignore": ["node_modules", ".git"], "ext": "js,json", "exec": "node src/index.js" }watchlists directories or files to monitor.srcis relative to the project root.ignorelists glob patterns that should never trigger a restart. The two entries above prevent any event fromnode_modulesor.git.extlimits the file extensions that trigger a restart. This avoids watching binary assets.execspecifies the command that Nodemon runs. Adjust if your entry point differs.
- Run Nodemon with the config file.
npx nodemon --config nodemon.jsonBecause
nodemon.jsonis in the project root, you can also simply typenpx nodemonand Nodemon will automatically pick it up.
Expected Checks
Use the following tests to confirm the configuration is active.
- Basic restart test: Edit
src/index.js(or any file insidesrc). Nodemon should log:[nodemon] restarting due to change in /path/to/project/src/index.js - Ignore test: Create a file
node_modules/dummy.jsand modify it. No restart log should appear. - Verbose mode: Run
npx nodemon --config nodemon.json --verbose. The output should include lines like:[nodemon] watching src [nodemon] ignoring node_modules, .git - Fallback test: Temporarily rename
nodemon.jsontonodemon.json.bakand runnpx nodemon. Nodemon will revert to its default behavior (watching all files). Editing any file should trigger a restart, confirming the original config was applied.
Recovery Options
If Nodemon behaves unexpectedly, try these steps:
- Validate JSON syntax. Use
jsonlint nodemon.jsonor an online validator to catch stray commas or missing quotes. - Check path case sensitivity. On Linux,
node_modulesand.gitmust match the exact case; on Windows the match is case‑insensitive. - Verify glob patterns. Nodemon uses minimatch. Ensure patterns are simple directory names; complex patterns may need escaping.
- Inspect the exec command. If Nodemon exits immediately, the
execstring may contain a typo. Run the command manually to confirm it works. - Use --verbose. This flag prints internal events and can reveal why a file change was ignored.
Limitations & Platform Notes
- On macOS, the default file‑system watcher (FSEvents) may still generate events for ignored directories if they are symlinked into
src. Avoid symlinks or use--legacy-watch. - Large
node_modulestrees on Linux can still cause a spike in events if the kernel’s inotify limit is reached. Increase/proc/sys/fs/inotify/max_user_watchesif you notice performance issues. - Glob patterns are case‑sensitive on Linux but case‑insensitive on Windows. Keep directory names consistent.
Practical Verification Checklist
| Check | Command | Expected Result |
|---|---|---|
| Run Nodemon | npx nodemon --config nodemon.json | Logs show "watching src" and "ignoring node_modules, .git" |
| Modify src file | Open src/index.js and change a line | Restarts, log of change path |
| Modify ignored file | Create/modify node_modules/dummy.js | No restart, no log |
| Test verbose | npx nodemon --config nodemon.json --verbose | Detailed watch/ignore events |
Conclusion
By confining Nodemon’s watch scope to src and ignoring node_modules and .git, you reduce unnecessary restarts and improve developer productivity. The nodemon.json approach keeps configuration declarative and version‑controlled, making it easy to share across team members.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.