错误:ENOSPC:文件观察器数量达到系统限制 角度

问题描述 投票:0回答:6

我在执行 Angular 10 项目时遇到此错误。

Error from chokidar (/myProject): Error: ENOSPC: System limit for number of file watchers reached, watch '/myProject/tsconfig.spec.json'

有办法解决这个错误吗?

angular linux inotify
6个回答
115
投票

您的 inotify 观察程序遇到了内核限制。您可以运行它来修复当前启动的问题,

sudo sysctl -w fs.inotify.max_user_watches=524288

您可以运行它来修复它以供将来启动,

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

17
投票

我在做 ssh 时在 vs code 中得到了这个。我认为问题是 VS Code 正在监视我的 node_modules 文件夹中的所有文件。为了在 vs code 中解决这个问题,我去了:

文件 > 首选项 > 设置,然后转到设置页面顶部的小纸张图标。这将带您进入 settings.json 文件与代码使用。然后我将其添加到设置文件中并解决了问题:

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

14
投票

我找到了这篇文章并帮助我解决了这个问题。您所要做的就是更改 max_user_watches

错误 ENOSPC 达到文件观察器数量的系统限制


7
投票

使用下面

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

增加系统的手表数量


7
投票

# 将新值插入系统配置

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

**#检查新应用的值**

cat /proc/sys/fs/inotify/max_user_watches

1
投票

观看许多文件可能会导致大量 CPU 或内存使用。因此,如果您确实需要,请增加文件观察器的数量。

以下是如何使用 Webpack 和 TypeScript 配置来优化文件监视:

使用Webpack

如果您使用webpackJs,那么我们可以使用在

webpack.config.js
中设置一些忽略文件路径来排除一些文件或文件夹,将其放入文件观察器中。

webpack.config.js

// webpack.config.js
// try below option for exclude the Node Module folder from File Watcher
module.exports = {
  //...
  watchOptions: {
    ignored: '**/node_modules',
    
    // for multiple file path, declare paths as array element
    // ignored: ['**/files/**/*.js', '**/node_modules'],
  },
};
// In addition, you can specify an absolute path:
watchOptions: {
    ignored: [path.posix.resolve(__dirname, './ignored-dir')],
},

欲了解更多详情请参阅此


使用 TsConfig

我们可以使用 tsconfig.json 文件的

excludeDirectories
选项中的 set
excludeFiles
watchOptions
选项排除某些文件或文件夹,将其放入文件观察器中。 (仅适用于 --watch。它可能不适用于没有 --watch 选项的
ng serve
命令)

tsconfig.json

// tsconfig.json
{
  // Some typical compiler options
  "compilerOptions": {
    "target": "es2020",
    "moduleResolution": "node"
    // ...
  },
  // NEW: Options for file/directory watching
  "watchOptions": {
    // Use native file system events for files and directories
    "watchFile": "useFsEvents",
    "watchDirectory": "useFsEvents",
    // Poll files for updates more frequently
    // when they're updated a lot.
    "fallbackPolling": "dynamicPriority",
    // Don't coalesce watch notification
    "synchronousWatchDirectory": true,
    
    // Finally, two additional settings for reducing the amount of possible
    // files to track  work from these directories
    "excludeDirectories": ["**/node_modules", "_build"],
    "excludeFiles": ["build/fileWhichChangesOften.ts"]
  }
}

欲了解更多详情请参阅此

© www.soinside.com 2019 - 2024. All rights reserved.