How to avoid "Too many open files" error in NodeJS

A

Ali Sherief

Guest
Hi guys, I know it's been quite a long time since I last posted here, but I have had this one development problem that had been bugging me for several months, until I finally found a solution for it this morning.

It specifically affects Linux desktops, but might also apply to Macs.

You might notice when you have a lot of browser tabs open like Chrome, Firefox, Opera and so on, when you try to use npm run dev on your Node project to run it locally, you get a hundred errors that all look like this:


Code:
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents/talksearch.io/app'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents/talksearch.io'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents/talksearch.io/app'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents/talksearch.io'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin/Documents'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/zenulabidin'
Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home'

And it just keeps printing this continuously forever. Alternatively, if you are not using NextJS, it will simply terminate npm.

This can be a very frustrating error to deal with if you have many open programs at once and don't want to close most of them, especially the browser.

The conventional solution that you will find on Stack Overflow and blogs is to change the maximum file limit of the current user, using ulimit programs. I will not cover these here, because they did not work for me. The problem was still the same.

What is the cause of Webpack EMFILE errors?​


The "too many open files" error is almost always caused by having too many inodes open on your system. An inode is just a file in Linux. It is the internal representation of the file.

Sometimes if you use VScode, you see this error very frequently and then it gives you some instructions on how to resolve the error by running sysctl commands in your terminal. It is the same type of error, and it is especially prevalent when using Webpack inside React projects.

How to fix Webpack EMFILE errors on Linux​


This is an approach I have only found when I was using Claude Code. I specifically instructed it to 'find a way to work around too many open files errors when running npm'.

What I have found is that it used set the environment variable: WATCHPACK_POLLING=true inside the terminal before running npm.

If you only want to run it once, use the following command: WATCHPACK_POLLING=true npm run dev.

Otherwise, if you want it to apply to all runs of npm run dev inside this terminal, run the command export WATCHPACK_POLLING=true in the shalle. Or place it in your .bashrc file.

That is all. I hope you found this helpful. Don't forget to follow me on X (@Zenul_Abidin) for more posts like this.

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top