【问题标题】:"jest --watch": Error: ENOSPC: System limit for number of file watchers reached, watch“jest --watch”:错误:ENOSPC:已达到文件观察者数量的系统限制,请观看
【发布时间】:2020-09-24 03:15:54
【问题描述】:

运行玩笑,首先没有参数,然后使用--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"]
  }
}

【问题讨论】:

    标签: node.js ubuntu jestjs filesystemwatcher file-watcher


    【解决方案1】:

    我在运行 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
    

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2022-01-22
      • 2019-09-09
      • 2021-03-25
      • 2019-06-29
      • 2019-05-24
      • 2020-11-18
      • 2021-04-07
      • 2020-10-08
      相关资源
      最近更新 更多