React Native 项目 Error: EMFILE: too many open files, watch

硬件:MacBook Pro (Retina, 13-inch, Mid 2014)

OS版本:MacOS BigSur 11.7.10 (20G1427)

更新: 删除modules的方法会有反弹,最后还是手动安装了预编译版本的watchman。

React Native 项目运行npm run web,出现如下错误:

shell 复制代码
Your macOS system limit does not allow enough watchers for Metro, install Watchman instead. Learn more: https://facebook.github.io/watchman/docs/install
Error: EMFILE: too many open files, watch

尝试了如下方案:

  1. 尝试 npm install watchman

    提示npm warn deprecated watchman@1.0.0: Package no longer supported. Contact support@npmjs.com for more info.

  2. 尝试brew install watchman

    无法安装,提示我的系统版本太老

  3. 尝试修改文件打开数量(需要进入恢复模式关闭SIP: csrutil disable ):
    sudo launchctl limit maxfiles 5000000 5000000

    通过launchctl limit maxfiles查看已经改好了,但是仍然提示同样错误。

  4. 删除模块:

shell 复制代码
rm -rf node_modules/
npm install

然后再次npm run web,终于可以了:

但是后面调试过程中安装了别的模块之后,这个问题又回来了。

  1. 手动安装watchman
    点击官方链接下载macOS的prebuild版本Release v2023.05.01.00 · facebook/watchman(后面已经没有新版本了)
    然后复制到/usr/local/Cellar/,
shell 复制代码
cd /usr/local/Cellar/
unzip watchman-v2023.05.01.00-macos.zip
mv watchman-v2023.05.01.00 watchman
cp /usr/local/Cellar/watchman/lib/*.*  /usr/local/lib
brew link watchman

此时如果运行watchman --version报错找不到,那就手动创建链接:

shell 复制代码
ln -s /usr/local/Cellar/watchman/*/bin/watchman /usr/local/bin/watchman

然后再次运行watchman --version检查。

然后watchman config(其实没有这个命令),会提示:

shell 复制代码
watchman config   
2025-01-05T11:12:13,985: [] while computing sockname: failed to create /usr/local/var/run/watchman/xxx-state: Permission denied

其中xxx是我的系统用户名。

所以需要这样:

shell 复制代码
sudo spctl --master-disable
sudo chown -R $(whoami) /usr/local/var/run/watchman

然后再次运行watchman config,会提示没这个命令

shell 复制代码
watchman config                                    
{
    "version": "20230430.125247.0",
    "error": "watchman::CommandValidationError: failed to validate command: unknown command config"
}

但是watchman可以正常工作了,npx expo start也没有文件打开太多的错误了。

可以使用watchman debug-status命令查看watchman状态:

shell 复制代码
watchman debug-status
ROOTS
-----
/Users/simonliu/Documents/AI/AICoding/cursor/projects/rn1
  - fstype: apfs
  - watcher: fsevents
  - uptime: 7709 s
  - crawl_status: crawl completed 7707330ms ago, and took 1864ms
  - done_initial: true

CLIENTS
-------
57960: 'watchman' 'debug-status'
  - since: 2025-01-05 13:24:42
  - state: dispatching command

54201: 'node' '/Users/simonliu/Documents/AI/AICoding/cursor/projects/rn1/node_modules/.bin/expo' 'start' '--reset-cache'
  - since: 2025-01-05 11:16:17
  - state: waiting for request

参考文献:

  1. ios - React Native Error: EMFILE: too many open files, watch - Stack Overflow
  2. MacOS binaries missing · Issue #1190 · facebook/watchman
相关推荐
你的人类朋友33 分钟前
【Node.js】什么是Node.js
javascript·后端·node.js
柳杉2 小时前
使用three.js搭建3d隧道监测-2
前端·javascript·数据可视化
刺客-Andy3 小时前
React 第七十节 Router中matchRoutes的使用详解及注意事项
前端·javascript·react.js
代码老y5 小时前
十年回望:Vue 与 React 的设计哲学、演进轨迹与生态博弈
前端·vue.js·react.js
zzywxc7875 小时前
详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
开发语言·javascript·人工智能·深度学习·金融·prompt·流程图
大明885 小时前
用 mouseover/mouseout 事件代理模拟 mouseenter/mouseleave
前端·javascript
林太白5 小时前
Nuxt.js搭建一个官网如何简单
前端·javascript·后端
晴空雨5 小时前
一个符号让 indexOf 判断更优雅!JavaScript 位运算的隐藏技巧
前端·javascript
前端snow5 小时前
前端无接口实现Table导出Excel的两种方案(附完整代码)
javascript·vue.js·react.js
古夕6 小时前
my-first-ai-web_问题记录03——NextJS 项目框架基础扫盲
前端·javascript·react.js