248

I'm learning GraphQL and am using prisma-binding for GraphQL operations. I'm facing this nodemon error while I'm starting my Node.js server and its giving me the path of schema file which is auto generated by a graphql-cli. What is this error 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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rehan Sattar
  • 3,497
  • 4
  • 13
  • 21
  • 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

12 Answers12

558

Updated (05/2023)

There are some tips in the comments and I brought them to update this question. If you're facing this problem, then you are probably using a Linux distro and your project is hitting your system's file watchers limit.

Check your current inotify file watch limit by executing:

$ cat /proc/sys/fs/inotify/max_user_watches

You can set a new limit temporarily with:

$ sudo sysctl fs.inotify.max_user_watches=131070
$ sudo sysctl -p

Or you can set a permanent limit:

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

Here we are setting 131070 file limit (as suggested by @Dmitriy in the comments). If this limit doesn't works for your system you can twice this number.

This documentation provides more technical details.

Old answer

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
Sujit
  • 1,653
  • 2
  • 9
  • 25
Isac Moura
  • 5,940
  • 3
  • 13
  • 27
  • 10
    use `sysctl --system` to reload for more recent systems – YLJ Mar 06 '20 at 13:03
  • 36
    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
  • 14
    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
  • 7
    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
  • Note that changing `/etc/sysctl.conf` may result in your change conflicting with or being overwritten by an OS upgrade, depending on your distribution. Better is to use a separate file in `/etc/sysctl.d/`, as [described in this answer](https://stackoverflow.com/a/55411444/107294). – cjs Sep 22 '22 at 09:33
  • Doesn't work for me sadly! – Jamie Hutber Nov 02 '22 at 15:18
  • This helped me out in a situation where I was upgrading `Laravel 8` > `Laravel 9` and swapping out `Laravel Mix` for `vite` & `Laravel Vite`. The `vite build` command worked fine, but `vite` caused the `ENOSPC: System limit for number of file watchers reached`. Running the above command fixed `vite`, and I didn't have to reboot my machine, or my hosts! – treckstar Nov 30 '22 at 02:25
  • On Ubuntu 22.04, I had to name the file `50-user-watches.conf` to make sure it got priority. To confirm the priority of what setting is being applied, run `sudo sysctl --system | grep max_user_watches -B2` and you'll see what setting is applied last and by which file. – mofojed Apr 06 '23 at 13:23
  • The "technical implications" link above from @IsacMoura can now be found here: https://github.com/guard/listen/blob/fd85e1cb2375767e3cbc4b5743ff50061e8a6c75/README.md#the-technical-details – Alex May 03 '23 at 02:36
  • Folks, thanks for the comments. I updated this question following the suggestions. 500K of limit is a huge limit (even though the documentation suggest that size) and I updated following the suggestion of @Dmitriy – Isac Moura May 03 '23 at 16:53
  • To "twice" a number, you double it and add 1. `twice(x): 2x + 1` – Chris Wesseling Jul 07 '23 at 09:46
74

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 --system. (On older systems you may need to use sudo sysctl -p instead.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cjs
  • 25,752
  • 9
  • 89
  • 101
  • 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
  • 1
    Putting this here for completeness `echo fs.inotify.max_user_watches=100000 | sudo tee /etc/sysctl.d/10-user-watches.conf && sudo sysctl -p`. – Ava Oct 21 '19 at 21:30
  • 9
    use `sysctl --system` to reload for more recent systems – YLJ Mar 06 '20 at 13:03
  • Is there a way to see the current value before updating it (eg to double the current value instead of using an arbitrary 100000)? Edit: it is `sudo sysctl fs.inotify.max_user_watches`, on my machine default is 65536 – Eric Burel Sep 28 '22 at 06:58
  • Also this answer seems to not work for Linux (either outdated or Mac-only?), see answer below using `/etc/sysctl.conf` file instead – Eric Burel Sep 28 '22 at 09:58
  • 1
    @EricBurel It's a Linux-only answer; I've never tried this on Mac. However, the option to reload all sysctl files was incorrect; I've fixed it. – cjs Sep 28 '22 at 13:22
70

I sometimes get this issue when working with Visual Studio Code on my Ubuntu machine.

In my case the following workaround helps:

Stop the watcher, close Visual Studio Code, start the watcher, and open Visual Studio Code again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Juri Sinitson
  • 1,445
  • 1
  • 14
  • 18
28

In order to test the changes, I temporary set 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manuel Lazo
  • 745
  • 7
  • 7
15

I had the same problem. However, mine was coming from Webpack. Thankfully, they had 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:

File webpack.config.js

module.exports = {
  watchOptions: {
    ignored: /node_modules/
  }
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sage
  • 193
  • 1
  • 7
9

This is a problem of inotify (inode notify) in the Linux kernel, so you can resolve it by using this command:

  1. For a temporary solution until rebooting the pc, use the following command

    sudo sysctl -w fs.inotify.max_user_watches=100000
    
  2. A permanent solution: To make this permanent, add a file named /etc/sysctl.d/10-user-watches.conf with the following contents:

    fs.inotify.max_user_watches = 10000
    

After making the change, reload the settings from all sysctl configuration files in /etc with sudo sysctl -p.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Ruturaj
  • 91
  • 1
  • 2
4

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

My nodemon command:

nodemon server.js via npm start

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
alramdein
  • 810
  • 2
  • 12
  • 26
3

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
  • 9,518
  • 6
  • 36
  • 58
3

I think most answers given here are correct, but using the systemctl command to restart my service solved the problem for me. Check the command below:

sudo systemctl restart systemd-sysctl.service
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gabriel soft
  • 432
  • 3
  • 7
  • There is nothing in the question about the platform. Can you [add](https://stackoverflow.com/posts/73668928/edit) the Linux distribution, version, etc. to the answer (but ********************* ***without*** ********************* "Edit:", "Update:", or similar - the answer should appear as if it was written today)? – Peter Mortensen Oct 23 '22 at 19:00
1

I use Vite alongside with Python and its venv module, and I encountered the same error because of a large venv folder (10k files)
Vite config allows ignoring directories from file watcher, so with venv ignored, the error was gone.

export default defineConfig({
  server: {
    watch: {
      ignored: ['**/venv/**'],
    }
  },
})  

Vite docs

Serguei A
  • 334
  • 1
  • 2
  • 10
0

You should follow answers such as this one:

cjs'

Or:

Isac Moura's

And for latest Ubuntu versions, run sudo sysctl --system to read these settings anew.

However, in my case, my changes to these configuration files were not picked up, because I had already tweaked these settings a while ago... and forgot about it. And I had placed the conflicting configuration file in the wrong place.

According to man sysctl.d, these settings can be placed in /etc/sysctl.d/*.conf, /run/sysctl.d/*.conf and /usr/lib/sysctl.d/*.conf.

In my case I had two files:

/etc/sysctl.d/10-user-watches.conf
/usr/lib/sysctl.d/30-tracker.conf     <<< Older file, with lower limit

Due to the naming convention, my older file was read last, and took precedence.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Will59
  • 1,430
  • 1
  • 16
  • 37
-4

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

  • 10
    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