我已经设置了一个新的空白反应本机应用程序。

在安装了几个节点模块后,我得到了这个错误。

Running application on PGN518.
internal/fs/watchers.js:173
   throw error;
   ^

Error: ENOSPC: System limit for number of file watchers reached, watch '/home/badis/Desktop/react-native/albums/node_modules/.staging'
   at FSWatcher.start (internal/fs/watchers.js:165:26)
   at Object.watch (fs.js:1253:11)
   at NodeWatcher.watchdir (/home/badis/Desktop/react-native/albums/node modules/sane/src/node watcher. js:175:20)
   at NodeWatcher.<anonymous> (/home/badis/Desktop/react-native/albums/node modules/sane/src/node watcher. js:310:16)
   at /home/badis/Desktop/react-native/albums/node modules/graceful-fs/polyfills.js:285:20
   at FSReqWrap.oncomplete (fs.js:154:5)

我知道这与没有足够的空间让守望者监视所有文件更改有关。

我想知道在这里采取的最佳行动是什么?

我应该忽略node_modules文件夹添加到.watchmanconfig ?


当前回答

迟回答,已经有很多好的答案了。

如果你想要一个简单的脚本来检查最大文件监视是否足够大,如果不够大,就增加限制,下面是:

#!/usr/bin/env bash

let current_watches=`sysctl -n fs.inotify.max_user_watches`

if (( current_watches < 80000 ))
then
  echo "Current max_user_watches ${current_watches} is less than 80000."
else
  echo "Current max_user_watches ${current_watches} is already equal to or greater than 80000."
  exit 0
fi

if sudo sysctl -w fs.inotify.max_user_watches=80000 && sudo sysctl -p && echo fs.inotify.max_user_watches=80000 | sudo tee /etc/sysctl.d/10-user-watches.conf
then
  echo "max_user_watches changed to 80000."
else
  echo "Could not change max_user_watches."
  exit 1
fi

脚本将限制增加到80000,但是可以随意设置您想要的限制。

其他回答

如果你已经添加了2个修复:fs.inotify.max_user_watches=524288

重启机器,一切都会恢复正常的 将引起问题的文件夹(对我来说是node_modules)重命名为任意名称(node_modilesa),然后重新命名。这将删除linux放在这些文件夹上的手表。允许您再次正常编码。

我通过使用sudo解决了这个问题 即

sudo yarn start

or

sudo npm start

使用sudo来解决这个问题将迫使观察者的数量增加,而不应用任何修改系统设置。不建议使用sudo来解决这类问题,尽管这是一个必须由您做出的选择,希望您明智地选择。

你可以修复它,增加inotify观察者的数量。

如果你对技术细节不感兴趣,只想要听工作:

如果您正在运行Debian、RedHat或其他类似的Linux发行版,请在终端中运行以下命令: $ echo fs.inotify。Max_user_watches =524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p 如果您正在运行ArchLinux,请运行以下命令 $ echo fs.inotify。Max_user_watches =524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl——系统

然后将其粘贴到您的终端,并按enter键运行它。


技术细节

Listen在Linux上默认使用inotify来监视目录的更改。系统限制可以监视的文件数量是很常见的。例如,Ubuntu Lucid(64位)的inotify限制设置为8192。

你可以通过执行以下命令来获得当前inotify文件的监视限制:

$ cat /proc/sys/fs/inotify/max_user_watches

当此限制不足以监视目录中的所有文件时,必须增加该限制以使Listen正常工作。

你可以临时设置一个新的限制:

$ sudo sysctl fs.inotify.max_user_watches=524288
$ sudo sysctl -p

如果你想让你的限制永久化,可以使用:

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p

如果listen一直抱怨,您可能还需要注意max_queued_events和max_user_instances的值。

来自官方文件:

“Visual Studio Code无法监视此大工作区中的文件更改”(错误ENOSPC)

当你看到这个通知时,它表明VS Code文件监视器正在用完句柄,因为工作区很大,包含很多文件。可以通过运行以下命令查看当前限制:

猫/proc/sys/fs/inotify/max_user_watches

可以通过编辑将限制增加到最大值

/etc/sysctl.conf

并将这一行添加到文件的末尾:

fs.inotify.max_user_watches = 524288

然后可以通过运行加载新值

Sudo sysctl -p

请注意Arch Linux的工作方式略有不同,详细信息请参见增加inotify观察者的数量。

虽然524,288是可以观看的最大文件数量,但如果您处于内存特别受限的环境中,您可能希望降低这个数字。每个文件手表占用540字节(32位)或~1kB(64位),因此假设所有524,288块手表都被消耗掉了,结果是大约256MB(32位)或512MB(64位)的上限。

另一个选择

是排除特定的工作空间目录从VS Code文件监视器的文件。watcherExclude设置。文件的默认值。watcherExclude排除了node_modules和.git下的一些文件夹,但是你可以添加其他你不想让VS Code跟踪的目录。

"files.watcherExclude": {
    "**/.git/objects/**": true,
    "**/.git/subtree-cache/**": true,
    "**/node_modules/*/**": true
  }

删除react node_modules

rm -r node_modules

yarn or npm install

yarn start or npm start

如果发生错误,请再次使用此方法