How to Fix the “System limit for number of file watchers reached” Error

The error “system limit for number of file watchers reached” can often occur while working on various environments such as Node.js, Visual Studio Code, React Native, and Angular.JS. Environments like these are used to develop applications using various languages, usually javascript. This post demonstrates the reasons to fix the error “System limit for number of file watchers reached” and also provides the solution to these reasons.

Resolve “system limit for number of file watchers reached” Problem

There exist just a couple of causes that will lead to this error popping up. Let us have a look at what those reasons can be.

Reason 1: Not Enough Watch Limit

In most cases, there is one major reason that invokes the “system limit for the number of file watchers reached” error. This reason revolves around not having enough Watcher Limit to watch for all the changes being made to the files in an environment. We can check the current maximum number of watchers by running the following command as shown below:

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

The number that appears is your maximum current limit of watchers, which is 65536 in the scenario above. Since the standard limit is often 524228, if your limit shows below that, then this may cause an error.

Solution: Increase Maximum Watchers Limit

The solution to this problem is to increase the maximum limit of watchers that we have for our files. Follow these steps to increase the limit.

Step 1: Increase the limit

Enter the following command into the “/etc/sysctl.conf” file to increase the maximum limit using the following command:

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf

This will increase the previous value in the file to 524288.

Step 2: Load value

You can load the new value into the file by running this command:

$ sudo sysctl -p

The following is an example of the commands being implemented:

Reason 2: Multiple Open Projects

When working with most of these environments such as the react native app, any change that we make can be observed clearly. This means that the watchers can see the change on the fly. So, when we have multiple projects open at once, this error can be invoked because of the increase in the number of watchers with an increase in the number of opened projects.

Solution: Close Other Projects

To fix the error invoked by reason 2, we need to close any unnecessary files or projects that are opened in the background. Or an easier way is to reboot the system. This will close the projects, and once the system turns on, you can open only the project that you need.

Conclusion

Two main reasons invoke the error. One of the reasons is reaching the maximum limit of watchers, which can be increased using commands. The other reason is having too many projects opened simultaneously, which we can shut down to fix. In this post, you have learned the major causes behind this issue and also seen how these problems can be fixed.