How to fix Error: EMFILE: too many open files, watch in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025ยท 247 views

This error occurs when your Node.js application exceeds the operating system's limit on the number of file descriptors that can be opened simultaneously. File watchers like fs.watch() are particularly affected, especially in development environments using tools like Webpack, Jest, or file watching services.

Quick fix

Fixes EMFILE

# Check all inotify settings
sysctl fs.inotify

# Check specifically the max user watches
cat /proc/sys/fs/inotify/max_user_watches
  1. Check current inotify limits on Linux
  2. Increase inotify limits temporarily (Linux)
  3. Increase inotify limits permanently (Linux)
  4. Increase file descriptor limits (macOS/Linux)
node.js โ€” error
# Check all inotify settings
sysctl fs.inotify
# Check specifically the max user watches
cat /proc/sys/fs/inotify/max_user_watches
Error: EMFILE: too many open files, watch

What this error means

The EMFILE error is a system-level error indicating that your application has reached the maximum number of file descriptors the operating system allows a single process to open simultaneously. In Node.js, file descriptors are used not only for files on disk, but also for network sockets, pipes, and other I/O resources. When using fs.watch() or libraries that depend on it (like Webpack dev server, Jest in watch mode, or React Native Metro bundler), each watched file or directory consumes a file descriptor. On Linux systems, this limit is controlled by the inotify subsystem, which has built-in kernel limits on how many watches can be active per user. The default limit on most Linux distributions is 8,192 watches per user, which can be quickly exhausted when watching large folder structures like node_modules or monorepos with thousands of files. macOS uses FSEvents instead of inotify and typically doesn't face this limitation.

Typical symptoms

  • Development server (Webpack, Vite) fails to start or crashes during file watching
  • Jest tests in watch mode fail with EMFILE error
  • File watcher stops detecting changes in development
  • Error message mentions "too many open files" or "watch" in the stack trace
  • Problem appears when working with large projects or monorepos
  • Issue is more common on Linux systems, rarely occurs on macOS

Common causes

  1. System file descriptor limit (ulimit) is too low for the number of files being watched
  2. Linux inotify limits (max_user_watches) are exhausted by watching large directory trees
  3. Multiple development tools running simultaneously, each consuming file watchers
  4. Watching node_modules directory or other large folder structures unnecessarily
  5. Application not properly closing file handles after use
  6. Running multiple Node.js projects in watch mode on the same system

How to fix "Error: EMFILE: too many open files, watch"

1Check current inotify limits on Linux

First, verify your current system limits to understand what you're working with:

bash
# Check all inotify settings
sysctl fs.inotify

# Check specifically the max user watches
cat /proc/sys/fs/inotify/max_user_watches

The default is typically 8,192, which may be insufficient for modern JavaScript development.

2Increase inotify limits temporarily (Linux)

For a quick fix that lasts until the next reboot:

bash
# Increase to 524,288 watches (common recommendation)
sudo sysctl fs.inotify.max_user_watches=524288
sudo sysctl -p

This will immediately take effect but won't persist after a system restart.

3Increase inotify limits permanently (Linux)

To make the change permanent across reboots:

bash
# Add configuration to sysctl.conf
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
echo fs.inotify.max_user_instances=1024 | sudo tee -a /etc/sysctl.conf

# Reload sysctl configuration
sudo sysctl -p

You may also want to increase max_user_instances if you run multiple watching applications simultaneously.

4Increase file descriptor limits (macOS/Linux)

If you're hitting general file descriptor limits (not inotify-specific):

macOS:

bash
# Check current limit
ulimit -n

# Increase limit for current session
ulimit -n 65536

Linux:

bash
# Check current soft and hard limits
ulimit -Sn
ulimit -Hn

# Increase for current session
ulimit -n 65536

For permanent changes, edit /etc/security/limits.conf and add:

bash
* soft nofile 65536
* hard nofile 65536
5Use Watchman as an alternative file watcher

Watchman is a file-watching service from Facebook that handles large file trees more efficiently:

bash
# Install Watchman (macOS)
brew install watchman

# Install Watchman (Linux - Ubuntu/Debian)
sudo apt-get install watchman

# Configure Jest to use Watchman
# In your jest.config.js or package.json:
{
  "watchman": true
}

Watchman is particularly effective with Jest and React Native projects.

6Exclude unnecessary directories from watching

Configure your build tools to avoid watching large directories that don't need monitoring:

Webpack:

javascript
// webpack.config.js
module.exports = {
  watchOptions: {
    ignored: /node_modules/,
    // Or more specific exclusions:
    ignored: ['**/node_modules', '**/dist', '**/.git']
  }
};

Vite:

javascript
// vite.config.js
export default {
  server: {
    watch: {
      ignored: ['**/node_modules/**', '**/dist/**']
    }
  }
};

Jest:

json
// jest.config.js
{
  "watchPathIgnorePatterns": ["node_modules", "dist", "build"]
}
7Verify the fix

After applying changes, verify the issue is resolved:

bash
# Check the new limit
cat /proc/sys/fs/inotify/max_user_watches

# Restart your development server
npm run dev
# or
yarn dev

# Monitor file descriptor usage
lsof -p $(pgrep -f "node") | wc -l

If you still encounter issues, you may need to increase the limits further or investigate if there's a file handle leak in your application.

Advanced notes

Platform Differences:
macOS uses FSEvents for file system monitoring, which has no practical upper limit and scales much better than Linux's inotify. This is why EMFILE errors related to file watching are far more common on Linux systems. Windows uses ReadDirectoryChangesW, which has different limitations.

Calculating Required Watches:
If you want to estimate how many watches you need, count the files in your project:

bash
find . -type f | wc -l

Then multiply by 1.5-2x to account for directories and buffer. For example, a 50,000 file project should set max_user_watches to at least 100,000.

Docker Considerations:
When running Node.js in Docker containers, the inotify limits are inherited from the host system. You'll need to increase the limits on the Docker host, not inside the container. This is a common source of confusion when developing with Docker.

File Handle Leaks:
If increasing limits doesn't help, you may have a file handle leak. Use lsof to monitor open file descriptors:

bash
# Watch file descriptor count in real-time
watch -n 1 'lsof -p $(pgrep -f "node") | wc -l'

If the count continuously increases without bound, you have a leak and should review your code for unclosed streams or file handles.

Monorepo Considerations:
In monorepos with multiple packages, each package's dev server may set up its own watchers. Consider using a tool like Turborepo or Nx which provide smarter file watching strategies and can share file system information across packages.

Frequently asked questions

What does "EMFILE" mean?
The EMFILE error is a system-level error indicating that your application has reached the maximum number of file descriptors the operating system allows a single process to open simultaneously. js, file descriptors are used not only for files on disk, but also for network sockets, pipes, and other I/O resources. watch() or libraries that depend on it (like Webpack dev server, Jest in watch mode, or React Native Metro bundler), each watched file or directory consumes a file descriptor.
How do I fix "EMFILE"?
This error occurs when your Node.js application exceeds the operating system's limit on the number of file descriptors that can be opened simultaneously. File watchers like fs.watch() are particularly affected, especially in development environments using tools like Webpack, Jest, or file watching services.
What causes "EMFILE"?
System file descriptor limit (ulimit) is too low for the number of files being watched Linux inotify limits (max_user_watches) are exhausted by watching large directory trees Multiple development tools running simultaneously, each consuming file watchers
Is the "EMFILE" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading