System limit for number of file watchers reached [Solved]

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

System limit for number of file watchers reached [Solved] #

The error "System limit for number of file watchers reached" occurs when your system has reached its limit for the number of files being watched. To solve the error, increase the limit of inotify watchers on your system.

system limit for number of file watchers reached

shell
Watchpack Error (watcher): Error: ENOSPC: System limit for number of file watchers reached, watch '/home/borislav/Documents/one/projects'

Open your shell and run one of the following commands:

shell
# for Debian, RedHat and other similar Linux distros echo fs.inotify.max_user_watches=393210 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p # for ArchLinux echo fs.inotify.max_user_watches=393210 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

increase amount of inotify watchers

You can read more about increasing the amount of inotify watchers on your Linux system in this Github repository.

When running your development server or watching your tests, you have exceeded your system's limit for the number of files being watched.

This often happens when using Nodemon, React.js, Angular, Jest, Webpack or other similar packages that tend to watch a lot of files.

The way to solve the error is to increase the limit of inotify watchers.

The command fs.inotify.max_user_watches=393210 is used to increase the amount of inotify watchers on your system.

However, the changes won't get persisted between reboots unless you write the new amount of watchers to a file.

You can check the current amount by running the following command:

shell
cat /proc/sys/fs/inotify/max_user_watches

get amount of inotify watchers

The default number of inotify watchers in Ubuntu 20.04 is 65535.

393210 is 65535 multiplied by 6. You can set a more conservative number of inotify watchers if you'd like and increase the number when necessary.

The tee command reads the input and writes it to standard output and one or more files.

shell
# for Debian, RedHat and other similar Linux distros echo fs.inotify.max_user_watches=393210 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p # for ArchLinux echo fs.inotify.max_user_watches=393210 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

We used the -a (append) flag to append to the file and not overwrite it.

If you cat the /etc/sysctl.conf (Debian, RedHat) or /etc/sysctl.d/40-max-user-watches.conf (ArchLinux) file on your system, you will see the line that increases the number of inotify watchers at the end.

The sudo sysctl -p command is used to instruct sysctl to read the values from the /etc/sysctl.conf file.

The sysctl --system command does the same if you use ArchLinux.

In its entirety, the command:

  1. Increases the number of inotify watchers.
  2. Writes the new amount to a file.
  3. Instructs sysctl to read the updated values.

You can view the updated amount by running the following command:

shell
cat /proc/sys/fs/inotify/max_user_watches

You should be able to run your development server or your test server after you've made the change.

If the error persists, try setting a higher number of inotify watchers.

shell
# for Debian, RedHat and other similar Linux distros echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p # for ArchLinux echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

Try restarting your Nodemon, React, Angular or Jest server after you've updated the amount of inotify watchers on your system.

If you got the error when using VSCode to watch files, try closing and reopening the VSCode editor.

If you got the error when using Webpack, try excluding the node_modules folder from being watched by setting the watchOptions property in your webpack.config.js file.

webpack.config.js
module.exports = { //... watchOptions: { ignored: /node_modules/, }, };

This will exclude the node_modules folder from being watched.

If none of the suggestions helped, try deleting your node_modules directory and re-install the dependencies.

shell
# 👇️ delete node_modules directory rm -rf node_modules # 👇️ clean npm cache npm cache clean --force # 👇️ if you use npm npm install # 👇️ if you use yarn yarn # 👇️ restart your development server

Restart your development server after removing the node_modules directory and re-installing your project's dependencies.

Conclusion #

The error "System limit for number of file watchers reached" occurs when your system has reached its limit for the number of files being watched. To solve the error, increase the limit of inotify watchers on your system.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.