Fix 'EMFILE: too many open files, watch' in React Native
By Flavio Copes
Learn how to fix the EMFILE too many open files watch error in React Native on macOS by installing the watchman utility with Homebrew.
The EMFILE: too many open files, watch error in React Native on macOS is fixed by installing watchman with Homebrew. Here’s the story, and why it works.
I was starting a React Native project on my MacBook Air but when running the command:
npx react-native start
I got an error that contained this line:
Error: EMFILE: too many open files, watch
Why does this error happen?
That command starts Metro, the React Native bundler. To provide hot reloading, Metro watches every file in your project for changes, and a React Native project contains thousands of files once you count node_modules.
Without a better tool available, Metro falls back on Node’s built-in file watching, which keeps a file descriptor open for each watched file. macOS puts a fairly low limit on how many files a process can hold open at once. Cross that limit and the operating system refuses to open more, which surfaces as EMFILE: too many open files.
So the error is not about your code. It’s the file watcher running out of a system resource.
The fix: install watchman
I tried various ways to solve it, until I found the suggestion to install the watchman utility using Homebrew.
I ran
brew install watchman
and that fixed the problem, because React Native internally was able to use watchman to watch file changes (used to provide hot reloading in the app to refresh it when a file is changed).
watchman is a file watching service built by Facebook, the same company behind React Native, and Metro picks it up automatically when it’s installed. No configuration needed.
It’s much more efficient than the built-in file watching: it runs as a single background service that watches whole directory trees, instead of holding one file descriptor per file. That’s why the error disappears.
After installing, stop Metro and run npx react-native start again so it detects watchman.
Still seeing the error?
If the error comes back after the install, watchman may have stale state from previous runs. Clear it with:
watchman watch-del-all
then start Metro again.
You’ll find suggestions online to raise the open files limit with ulimit instead. That can work, but it treats the symptom. Installing watchman removes the cause, and it makes file watching faster too.
Related posts about react-native: