Introduction
The Linux inotify API allows applications to monitor filesystem events. Each watched file or directory consumes one watch handle from the kernel pool limited by fs.inotify.max_user_watches (default 8192). Modern development tools that watch entire project trees (webpack, gulp, nodemon, VS Code) can exhaust this limit quickly, causing file change detection to fail silently.
Symptoms
- webpack:
Error: ENOSPC: System limit for number of file watchers reached - nodemon:
Error: watch ENOSPC - VS Code:
The workspace contains more than 8192 files, watch failed dmesgshowsinotify_add_watch: too many open files- Applications stop detecting file changes without explicit errors
cat /proc/sys/fs/inotify/max_user_watchesshows 8192
Common Causes
- Large project directories with thousands of files (node_modules, vendor/)
- Multiple file watchers running simultaneously
- Default
max_user_watchesof 8192 too low for modern development - Watcher recursively monitoring directories with many subdirectories
- Docker bind mounts creating additional watch entries
Step-by-Step Fix
- 1.Check current inotify watch usage:
- 2.```bash
- 3.cat /proc/sys/fs/inotify/max_user_watches
- 4.# Count current watches in use
- 5.sudo find /proc/*/fd -lname anon_inode:inotify 2>/dev/null | wc -l
- 6.# See which processes hold watches
- 7.for pid in $(pgrep -f "node|webpack|vscode"); do
- 8.count=$(ls -la /proc/$pid/fd 2>/dev/null | grep -c inotify)
- 9.[ "$count" -gt 0 ] && echo "PID $pid: $count watches"
- 10.done
- 11.
` - 12.Increase the inotify watch limit:
- 13.```bash
- 14.# Temporary (until reboot)
- 15.sudo sysctl -w fs.inotify.max_user_watches=524288
- 16.# Verify
- 17.cat /proc/sys/fs/inotify/max_user_watches
- 18.
` - 19.Make the change persistent:
- 20.```bash
- 21.echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.conf
- 22.echo "fs.inotify.max_queued_events=16384" | sudo tee -a /etc/sysctl.conf
- 23.echo "fs.inotify.max_user_instances=1024" | sudo tee -a /etc/sysctl.conf
- 24.sudo sysctl -p
- 25.
` - 26.Optimize watcher exclude patterns (webpack example):
- 27.```javascript
- 28.// webpack.config.js
- 29.module.exports = {
- 30.watchOptions: {
- 31.ignored: /node_modules|\.git|\.svn|vendor/
- 32.}
- 33.};
- 34.
` - 35.Configure VS Code to exclude directories:
- 36.```json
- 37.// settings.json
- 38.{
- 39."files.watcherExclude": {
- 40."/node_modules/": true,
- 41."/.git/objects/": true,
- 42."/dist/": true
- 43.}
- 44.}
- 45.
` - 46.Verify the fix by restarting the affected application:
- 47.```bash
- 48.# Restart webpack
- 49.npm run dev
- 50.# Check no ENOSPC errors in output
- 51.
`
Prevention
- Set
max_user_watches=524288as a baseline on development machines - Exclude
node_modules,.git,vendor,distfrom file watchers - Use polling-based watching (
watchOptions.poll) as a fallback for network filesystems - Monitor inotify usage in team onboarding documentation
- Use
inotifywatchfrominotify-toolsto profile watch usage