Troubleshooting guide
EMFILE: too many open files in Node.js
Your process has used up its file descriptors. Sockets count as well as files, so servers can hit this with very few files actually open.
3 min read10 sectionsUpdated
On this page
# Limit and current count for a process
cat /proc/PID/limits | grep 'open files'
ls /proc/PID/fd | wc -l
# Kinds of descriptors
sudo lsof -p PID | awk '{print $5}' | sort | uniq -c | sort -rn
# Most common targets
sudo lsof -p PID | awk '{print $9}' | sort | uniq -c | sort -rn | headFixes
Limit concurrency using a library such as p-limit, so that only a set number of operations run at once. Always destroy streams using stream.pipeline, which handles errors. Reuse connections with a keep-alive agent. Raise the limit with LimitNOFILE=65536 in systemd, and --ulimit nofile for Docker. For development, ignore node_modules in your watcher.
EMFILE or ENFILE?
EMFILE is a per-process limit, whereas ENFILE means the whole system ran out, controlled by fs.file-max. The distinction matters because raising a per-process limit does nothing about a system-wide shortage. On macOS, the watcher error often appears as EMFILE because of kern.maxfiles. Tools such as graceful-fs queue operations when they hit the limit, which hides the error in scripts but does nothing for a real leak. A descriptor count that only ever goes up is a leak, however generous the limit may be.
Catch it automatically
Chart open descriptors as a percentage of the limit per process, and alert at 80 percent. Your log management can alert on EMFILE text, and Node.js monitoring gives the rest of the picture.
How do I raise the limit?
Use ulimit -n for a shell, LimitNOFILE in systemd, and --ulimit for Docker.
Why does npm install cause EMFILE?
Many files open at once with a low limit. Raise the limit.
What is the difference from EADDRINUSE?
EADDRINUSE is about a port being taken. EMFILE is about running out of descriptors.
Related reading
- MySQL: Too many connections: Fix MySQL error 1040 Too many connections: check max_connections and current threads, find sleeping or leaked connections, size application pools.
- Nginx "worker_connections are not enough": how to fix it: Fix Nginx worker_connections are not enough: why proxying uses two connections per client, how to size limits, raise descriptors and add upstream keepalive.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- Too many TIME_WAIT connections: what to do: Understand Linux TIME_WAIT sockets, when they cause ephemeral port exhaustion, why keep-alive is the real fix and which kernel settings not to touch.
- ERR_TOO_MANY_REDIRECTS: how to fix a redirect loop: Fix ERR_TOO_MANY_REDIRECTS: trace the chain with curl, find the loop between CDN, server and app, such as HTTPS or www rules, and prevent it.
- 429 Too Many Requests: what it means and how to fix it: 429 Too Many Requests explained: rate limits, the Retry-After header, backoff with jitter for clients, and how to set fair rate limits on your own API.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.