196

I'm learning graphql and using prisma-binding for graphql operations. I'm facing this nodemon error while I'm starting my node server and its giving me the path of schema file which is auto generated by a graphql-cli. Can anyone tell me what this error is all about?

Error:

Internal watch failed: ENOSPC: System limit for number of file watchers reached, watch '/media/rehan-sattar/Development/All projects/GrpahQl/graph-ql-course/graphql-prisma/src/generated
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Rehan Sattar
  • 2,757
  • 3
  • 11
  • 19
  • This is the linux ulimit error see here https://stackoverflow.com/questions/34588/how-do-i-change-the-number-of-open-files-limit-in-linux – Janith Dec 26 '18 at 09:54
  • Tried this! Getting the same error again! – Rehan Sattar Dec 26 '18 at 10:27
  • 2
    You are probably watching too many files. Maybe it's including the nod_modules directory as well? – Mikkel Dec 27 '18 at 00:10
  • `node_modules` are essential because all the packages are there. I've tried to kill the previous processes running on the port of my server, it worked for me but I don't know how long it will take now :D – Rehan Sattar Dec 27 '18 at 05:41

8 Answers8

446

If you are using Linux, your project is hitting your system's file watchers limit

To fix this, on your terminal, try:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
Isac Moura
  • 4,801
  • 3
  • 12
  • 25
  • 9
    use `sysctl --system` to reload for more recent systems – YLJ Mar 06 '20 at 13:03
  • 22
    is there any other implications that we must know when we do this? I knew this helps solve the issue, I tried it myself. But I am a bit skeptic what possible side effects this fix can cause. – Aldee May 15 '20 at 02:45
  • 1
    @Aldee about the technical implications of this change I recommend checking this wiki: https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details – Isac Moura May 15 '20 at 04:26
  • This also worked out a lot of issues with npm plugins. thx – The Bumpaster Jul 08 '20 at 21:00
  • Thank you! I had the same error on a React project I have just created and that has fixed it. –  Aug 27 '20 at 12:14
  • 10
    I wouldn't recommend increasing it so much if you're not sure how many are in use. Check the number in use with the following `find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'` – Nick Bull Sep 25 '20 at 12:16
  • I actually have `max_user_watches` 4288 and excluded in `nodemonconfig` in `package.json` `.git` and `node_modules`. I wonder why there are so many files still which lead to the error. – Timo May 25 '21 at 19:34
  • 3
    Default value (on Ubuntu 21) was 65535 and setting it to just twice that value (131070) fixed the Node JS issues for me. So according to the principle of minimizing side effects, it is worth trying smaller increments before going all the way to 500k. – Dmitriy Jul 19 '21 at 01:41
53

I sometimes get this issue when working with VSCode on my Ubuntu machine.

In my case the following workaround helps:

stop the watcher, close VScode, start the watcher, open VSCode again.

Juri Sinitson
  • 1,047
  • 11
  • 17
49

You need to increase the inotify watchers limit for users of your system. You can do this from the command line with:

sudo sysctl -w fs.inotify.max_user_watches=100000

That will persist only until you reboot, though. To make this permanent, add a file named /etc/sysctl.d/10-user-watches.conf with the following contents:

fs.inotify.max_user_watches = 100000

After making the above (or any other) change, you can reload the settings from all sysctl configuration files in /etc with sudo sysctl -p.

cjs
  • 23,786
  • 6
  • 86
  • 97
  • Thank you so much! Worked for me!! But where i have to add this file? – Rehan Sattar Apr 06 '19 at 09:11
  • @RehanSattar Create a file `/etc/sysctl.d/10-user-watches.conf` and in it put `fs.inotify.max_user_watches = 100000`. – cjs Apr 06 '19 at 10:32
  • Putting this here for completeness `echo fs.inotify.max_user_watches=100000 | sudo tee /etc/sysctl.d/10-user-watches.conf && sudo sysctl -p`. – RedHatter Oct 21 '19 at 21:30
  • 6
    use `sysctl --system` to reload for more recent systems – YLJ Mar 06 '20 at 13:03
20

In order to test the changes, I set temporary the parameter with the value 524288.

sysctl -w fs.inotify.max_user_watches=524288

then I proceed to validate :

npm run serve

And the problem was solved, in order to make it permanent, you should try to add a line in the file "/etc/sysctl.conf" and then restart the sysctl service :

cat /etc/sysctl.conf |tail -n 2
fs.inotify.max_user_watches=524288

sudo systemctl restart systemd-sysctl.service
Manuel Lazo
  • 557
  • 5
  • 7
9

I had the same problem, however mine was coming from webpack. Thankfully they hd a great solution on their site:

For some systems, watching many files can result in a lot of CPU or memory usage. It is possible to exclude a huge folder like node_modules using a regular expression:

webpack.config.js

module.exports = {
  watchOptions: {
    ignored: /node_modules/
  }
};
Sage
  • 123
  • 1
  • 6
2

It can be hard to know how much to increase the number of watchers by. So, here's a utility to double the number of watchers:

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

function set_inode_watchers() {
  sudo sysctl -w fs.inotify.max_user_watches="$1"
}

function double_inode_watchers() {
  watcher_count="$(get_inode_watcher_count)"
  set_inode_watchers "$((watcher_count * 2))"

  if test "$1" = "-p" || test "$1" = "--persist"; then
    echo "fs.inotify.max_user_watches = $((watcher_count * 2))" > /etc/sysctl.d/10-user-watches.conf
  fi
}

# Usage
double_inode_watchers
# to make the change persistent
double_inode_watchers --persist
Nick Bull
  • 8,827
  • 6
  • 26
  • 48
2

In my case, while I'm doing the nodemon command in the Linux server. I have my VSCode open (SSH to the server). So based on @Juri Sinitson's answer, I just close the VSCode and run the nodemon command again. And it works.

My nodemon command: nodemon server.js via npm start

alramdein
  • 666
  • 1
  • 9
  • 18
-4

On Linux, I've actually run with sudo. sudo npm start

  • 8
    This will often work because root usually has a much higher inotify watch limit than regular users, but it's a _very_ bad idea to be running things as root when they don't need to be. See [my answer to this question](https://stackoverflow.com/a/55411444/107294) for how to change the user limit. – cjs Mar 29 '19 at 06:12