1

Running jest, first with no arguments, then with the --watch flag.

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

What would cause the error Error: ENOSPC: System limit for number of file watchers reached, watch for a fairly small project?

Any suggestions on how to debug this are also welcome. I'm running 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"]
  }
}
Sean D
  • 3,810
  • 11
  • 45
  • 90

2 Answers2

3

I faced the same issue running MX Linux 19.3, Node.js 14.15.1, but using yarn instead of npm.

This is not related to node, jest or npm.

The file watcher watches all files, including the ones in the node_modules folder. So the number of file watches may exceed the default operating system configuration.

In my MX Linux, the default maximum allowed is 8192.

$ sysctl -n fs.inotify.max_user_watches
8192

I increased the maximum allowed value temporarily to 20000 using the following command.

$ sudo sysctl -w fs.inotify.max_user_watches=20000 && sudo sysctl -p

After I did the config change, jest --watch went through successfully without the error.

Then I checked the number of file watches in use by the following:

$ find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'
8207

Obviously, this is more than the default value of 8192.

When I ran yarn start, the number of file watches in use was much more:

$ find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'
17501

Therefore, a maximum value of 20000 looked reasonable to me.

In order to make this config permanent across system reboots, I went ahead and created a file /etc/sysctl.d/10-user-watches.conf with the desired limit:

fs.inotify.max_user_watches = 20000

It solved the problem.

Update

In another bigger project, the number of file watches was much more than 20000.

Therefore, it probably is better to set a higher limit, like 80000 or so.

echo fs.inotify.max_user_watches=80000 | sudo tee /etc/sysctl.d/10-user-watches.conf
HelloWorld101
  • 3,878
  • 2
  • 34
  • 47
0

I faced the same issue, And i fixed this issue, By deleting the existing node modules and again i have installed node modules then the issue resolved.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '22 at 12:37