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

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

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 ?


当前回答

Linux使用inotify包来观察文件系统事件、单个文件或目录。

由于React / Angular会在保存时热加载和重新编译文件,所以它需要跟踪所有项目的文件。增加inotify监视限制应该会隐藏警告消息。

你可以试着编辑

# insert the new value into the system config
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

# check that the new value was applied
cat /proc/sys/fs/inotify/max_user_watches

# config variable name (not runnable)
fs.inotify.max_user_watches=524288

其他回答

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

sudo yarn start

or

sudo npm start

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

请参考此链接[1]。Visual Studio代码中提到了对此错误消息的简要解释。我也遇到了同样的错误。在相关文件中添加以下参数将修复此问题。

 fs.inotify.max_user_watches=524288

[1] https://code.visualstudio.com/docs/setup/linux#_visual-studio-code-is-unable-to-watch-for-file-changes-in-this-large-wor kspace-error-enospc

验证最简单的方法,它是为我工作良好。

步骤1:停止服务器

第二步:手动删除' '下的'cache'文件夹。Angular的文件夹

步骤3:启动服务器

根本原因

上面的大多数回答都在谈论提高限制,而不是消除根本原因,这通常只是冗余监视的问题,通常是针对node_modules中的文件。

Webpack

答案在webpack 5文档中: watchOptions:{被忽略:/node_modules/}

只需在这里阅读:https://webpack.js.org/configuration/watch/#watchoptionsignored

文档甚至提到这是一个“提示”,引用如下:

如果观察对你不起作用,试试这个方法。这可能会有所帮助 NFS和VirtualBox、WSL、容器或机器中的问题 码头工人。在这些情况下,使用轮询间隔并忽略large 文件夹,如/node_modules/,以保持CPU的使用最小化。

VS代码

VS Code或任何代码编辑器也会创建大量的文件监视。默认情况下,其中许多是完全多余的。更多信息请点击:https://code.visualstudio.com/docs/setup/linux#_visual-studio-code-is-unable-to-watch-for-file-changes-in-this-large-workspace-error-enospc

你可以修复它,增加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的值。