System limit for number of file watchers reached [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# 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, depending on your OS distribution.

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

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=524288 is used to increase the amount of inotify watchers on your system.

However, the changes won't persist 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.

524288 is 65535 multiplied by 8. 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=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

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.

# Setting a higher number of inotify watchers

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=655350 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p # for ArchLinux echo fs.inotify.max_user_watches=655350 | 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 get the error when using VSCode to watch files, try closing and reopening the VSCode editor.

# Solve the error when using webpack

If you get 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 reinstall your dependencies.

shell
# ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install # ๐Ÿ‘‡๏ธ restart your development server

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

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.

Copyright ยฉ 2024 Borislav Hadzhiev