On Linux, sometimes we get following error while trying to run Hot Module Replacement (HMR) server such as Vite;
... Error: ENOSPC: System limit for number of file watchers reached ...
This type of error appears when our system has low inotify file watch limit e.g., 8,192. Linux systems usually keep the limit low due to memory consumption considerations. When we run ‘npm run dev‘ command in our node based projects, there are usually a sizeable number of files which are required to be monitored by HMR server and therefore this file watchers limit error occurs. You may enter following command in terminal to know your inotify file watch limit;
cat /proc/sys/fs/inotify/max_user_watches
You can fix this issue temporarily or permanently. In order to fix it temporarily enter following commands in the terminal;
sudo sysctl fs.inotify.max_user_watches=1048576
sudo sysctl -p
If you feel the increased limit is too much for you needs then you may enter lesser limit i.e., 524288.
In order to permanently set the limit, edit /etc/sysctl.conf file and change fs.inotify.max_user_watches variable therein. You may enter following commands in the terminal to accomplish this;
sudo sh -c "echo fs.inotify.max_user_watches=1048576 >> /etc/sysctl.conf"
sudo sysctl -p
Now the limit is set permanently.