node.js - "jest --watch": Error: ENOSPC: System limit for number of file watchers reached, watch

标签 node.js ubuntu jestjs filesystemwatcher file-watcher

开 Jest ,首先不带参数,然后使用 --watch旗帜。

owner@G700:~/cp/projectName$ npm run test

> project_name@1.0.0 test /home/owner/cp/projectName
> jest

 PASS  src/classes/setupWizard/__tests__/SetupRole.test.ts
  ✓ SetupRole (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.335 s
Ran all test suites.


owner@G700:~/cp/projectName$ npm run test

> project_name@1.0.0 test /home/owner/cp/projectName
> jest --watch

internal/fs/watchers.js:186
    throw error;
    ^

Error: ENOSPC: System limit for number of file watchers reached, watch '/home/owner/cp/projectName/node_modules/fast-json-stable-stringify/test'
    at FSWatcher.<computed> (internal/fs/watchers.js:178:26)                                                          
    at Object.watch (fs.js:1445:34)
    at NodeWatcher.watchdir (/home/owner/cp/projectName/node_modules/sane/src/node_watcher.js:159:22)
    at Walker.<anonymous> (/home/owner/cp/projectName/node_modules/sane/src/common.js:109:31)
    at Walker.emit (events.js:315:20)
    at /home/owner/cp/projectName/node_modules/walker/lib/walker.js:69:16
    at FSReqCallback.oncomplete (fs.js:163:23) {
  errno: -28,
  syscall: 'watch',
  code: 'ENOSPC',
  path: '/home/owner/cp/projectName/node_modules/fast-json-stable-stringify/test',                        
  filename: '/home/owner/cp/projectName/node_modules/fast-json-stable-stringify/test'                     
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project_name@1.0.0 test: `jest --watch`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the project_name@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/owner/.npm/_logs/2020-06-05T00_30_53_889Z-debug.log

什么会导致错误 Error: ENOSPC: System limit for number of file watchers reached, watch对于一个相当小的项目?

也欢迎任何有关如何调试的建议。我正在运行 Lubuntu 20.04、NodeJS 14.2.0、NPM 6.14.4。
// package.json

{
  "name": "project_name",
  "version": "1.0.0",
  "description": "",
  "main": "compiled/index.js",
  "scripts": {
    "test": "jest --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "@babel/preset-typescript": "^7.10.1",
    "@types/jest": "^25.2.3",
    "@types/node": "^14.0.5",
    "@types/readline-sync": "^1.4.3",
    "babel-jest": "^26.0.1",
    "jest": "^26.0.1",
    "ts-jest": "^26.0.0",
    "typescript": "^3.9.3"
  },
  "dependencies": {
    "@google-cloud/text-to-speech": "^2.3.0",
    "@google-cloud/translate": "^5.3.0",
    "readline-sync": "^1.4.10"
  },
  "jest" : {
    "preset" : "ts-jest"
    , "modulePathIgnorePatterns" : ["compiled"]
  }
}

最佳答案

我在运行 MX Linux 19.3、Node.js 14.15.1 时遇到了同样的问题,但使用的是 yarn 而不是 npm。
这与 node、jest 或 npm 无关。
文件监视器监视所有文件,包括 node_modules 中的文件。文件夹。所以文件监视的数量可能会超过默认的操作系统配置。
在我的 MX Linux 中,允许的默认最大值是 8192 .

$ sysctl -n fs.inotify.max_user_watches
8192
我暂时将最大允许值增加到 20000使用以下命令。
$ sudo sysctl -w fs.inotify.max_user_watches=20000 && sudo sysctl -p
在我进行配置更改后,jest --watch成功通过,没有错误。
然后我通过以下方式检查了正在使用的文件监视数量:
$ find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'
8207
显然,这超过了8192的默认值。 .
当我运行 yarn start ,正在使用的文件监视数量要多得多:
$ find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'
17501
因此,最大值20000在我看来是合理的。
为了使此配置在系统重新启动后永久有效,我继续创建了一个文件 /etc/sysctl.d/10-user-watches.conf具有所需的限制:
fs.inotify.max_user_watches = 20000
它解决了这个问题。
更新
在另一个更大的项目中,文件监视的数量远远超过 20000。
因此,最好设置一个更高的限制,例如 80000或者。
echo fs.inotify.max_user_watches=80000 | sudo tee /etc/sysctl.d/10-user-watches.conf

关于node.js - "jest --watch": Error: ENOSPC: System limit for number of file watchers reached, watch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62206460/

相关文章:

typescript - @types/jest index.d.ts 文件返回错误

javascript - 从本地node_modules执行pug

php - 家庭计算机上的 LAMP 堆栈作为公共(public) Web 服务器

node.js - VSCode上的pwa-node类型启动配置是什么?

linux - 在 Ubuntu 上,有没有办法自动将文件移动到另一个目录,因为它们是 SFTP 的?

Emacs FTP 不适用于 Ubuntu 中的某些站点

javascript - 从 javascript 使用 CapsLock 触发新的键盘事件

.spec 文件中无法识别 Angular 8+ tsconfig 路径别名

javascript - Node.js 脏我如何从 [Object object] 获取数据?

node.js - 如何使用 nodeJS 和 protobufjs 在 MQTT 中发送缓冲区或字节数组