Linux:如何修复 Watchpack Error (watcher): Error: ENOSPC: System limit for number of file watchers reached

问题

在 Linux 上,你看到类似以下的错误消息:

watchpack_error_output.txt
Watchpack Error (watcher): Error: ENOSPC: System limit for number of file watchers reached, watch '/home/uli/dev/myproject/node_modules/@primeuix'

解决方案

如果你的 IDE(如 Visual Studio Code)监视大型工作区(通常是 node_modules 目录),经常会遇到此错误。

通常只需增加限制即可:

increase_inotify_limits.sh
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
echo fs.inotify.max_user_instances=16384  | sudo tee -a /etc/sysctl.conf
echo fs.inotify.max_queued_events=32768 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

不过我还建议配置 VS Code 不监视这些路径:

vscode_watcher_settings.json
"files.watcherExclude": {
  "**/node_modules/**": true,
  "**/dist/**": true,
  "**/.git/**": true
}

Check out similar posts by category: Linux