Nodemon Error: "System limit for number of file watchers reached"
Asked Answered
E

13

272

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

Epilimnion answered 26/12, 2018 at 9:50 Comment(4)
This is the linux ulimit error see here #35088Helicograph
Tried this! Getting the same error again!Epilimnion
You are probably watching too many files. Maybe it's including the nod_modules directory as well?Nims
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 :DEpilimnion
L
612

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
Lai answered 5/4, 2019 at 21:1 Comment(16)
use sysctl --system to reload for more recent systemsBocage
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.Fullbodied
@Fullbodied about the technical implications of this change I recommend checking this wiki: github.com/guard/listen/wiki/…Lai
This also worked out a lot of issues with npm plugins. thxJairia
Thank you! I had the same error on a React project I have just created and that has fixed it.Threedecker
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'Surrey
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.Bybee
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.Grocer
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.Liborio
Doesn't work for me sadly!Remanent
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!Tambratamburlaine
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.Croton
The "technical implications" link above from @IsacMoura can now be found here: github.com/guard/listen/blob/…Deason
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 @GrocerLai
To "twice" a number, you double it and add 1. twice(x): 2x + 1Toomer
This works for me, without restarting my ubuntu system.Asyllabic
L
78

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.)

Liborio answered 29/3, 2019 at 6:11 Comment(7)
Thank you so much! Worked for me!! But where i have to add this file?Epilimnion
@RehanSattar Create a file /etc/sysctl.d/10-user-watches.conf and in it put fs.inotify.max_user_watches = 100000.Liborio
Putting this here for completeness echo fs.inotify.max_user_watches=100000 | sudo tee /etc/sysctl.d/10-user-watches.conf && sudo sysctl -p.Slum
use sysctl --system to reload for more recent systemsBocage
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 65536Beaut
Also this answer seems to not work for Linux (either outdated or Mac-only?), see answer below using /etc/sysctl.conf file insteadBeaut
@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.Liborio
U
75

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.

Underwrite answered 6/3, 2019 at 16:19 Comment(6)
That's right! Because of VSCode. It should be autosave mode.Asgard
this worked for me; developing with ember-cliMeill
As w/my focal box this happens intermittently and the VSCode restart is the fix w/out fiddling w/max-_user_watches. (note: When it is working, I typically see watchers in use well below 7k)Ridotto
I think I have the same problem but with my Nextcloud client. How do I stop and restart the watcher? I couldn't search for the right term to get a proper answer.Lactary
Same situation (Ubuntu/VS Code). It might help to close any folders in the folder tree that you're not currently working on, maybe even hiding the explorer altogether (not sure if VS Code has watchers for currently visible files and folders in Explorer).Embow
Same here with web storm in JetBrains (Ubuntu), restart/invalidating cache did the trick.Openmouthed
A
30

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
Apparitor answered 27/4, 2020 at 15:20 Comment(1)
This hint for test temporarily is priceless. Thank youMisapprehension
G
16

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/
  }
};
Gambetta answered 8/1, 2021 at 0:11 Comment(0)
L
10

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.

Leisurely answered 29/6, 2022 at 11:7 Comment(1)
Missing a zero in item 2Threequarter
C
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

Cathern answered 24/1, 2021 at 23:30 Comment(2)
Worked for me as well. I had basically two VSCode open. Closing the previous one solved the issue.Possessive
this surprisingly worked!Storeroom
S
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
Surrey answered 25/9, 2020 at 12:30 Comment(0)
H
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
Haematothermal answered 10/9, 2022 at 2:34 Comment(1)
There is nothing in the question about the platform. Can you add the Linux distribution, version, etc. to the answer (but ********************* without ********************* "Edit:", "Update:", or similar - the answer should appear as if it was written today)?Cindiecindra
F
2

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

Fustigate answered 30/4, 2023 at 16:3 Comment(0)
E
2
 sudo sysctl fs.inotify.max_user_watches=131070


 sudo sysctl -p 

If this limit doesn't works for your system you can twice this number.

Electronic answered 20/12, 2023 at 7:29 Comment(0)
I
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.

Ignacia answered 27/9, 2022 at 16:2 Comment(0)
P
-4

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

Projective answered 26/3, 2019 at 0:38 Comment(1)
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 for how to change the user limit.Liborio

© 2022 - 2024 — McMap. All rights reserved.