React Native报错ENOSPC:文件监视器达系统上限,求最优解决方案
Hey there, let's break down how to fix this frustrating error you're hitting. The ENOSPC issue pops up because your Linux system has a default cap on the number of files it can monitor for changes, and React Native + watchman are hitting that ceiling—especially with the massive number of files in node_modules.
Here's the optimal approach, combining system-level fixes and watchman optimization:
1. Permanently Increase the System File Watcher Limit
This addresses the root cause: your system's default watcher count is too low for large Node.js projects. Run this terminal command to update the limit (it’s permanent and takes effect right away):
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
This bumps the limit to 524,288 (a safe, dev-friendly value). You can confirm it worked by running cat /proc/sys/fs/inotify/max_user_watches.
2. Ignore node_modules in Watchman (Yes, Absolutely Do This!)
Adding node_modules to watchman’s ignore list is a no-brainer—you almost never need watchman to track changes in this directory (unless you’re actively modifying package source code, which is rare). This cuts down the number of files watchman has to monitor, lightening its workload and preventing future limit hits.
Create or edit a .watchmanconfig file in your project’s root directory with this content:
{ "ignore_dirs": ["node_modules"] }
Save the file, then restart your React Native dev server (npx react-native start) for the changes to kick in.
3. Bonus: Reset React Native Cache (If Needed)
Sometimes stale cache can cause unexpected file watcher glitches. If you still run into issues after the above steps, reset the cache with:
npx react-native start --reset-cache
Why This Combo Is Optimal
- The system limit increase fixes the underlying constraint, so you won’t hit the same wall with other large projects.
- Ignoring
node_modulesreduces watchman’s workload, making your dev server faster and less likely to trigger the limit again.
内容的提问来源于stack exchange,提问作者Bmaed Riasbet

