Skip to content

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

Key points

  • Sockets and pipes count as open files.
  • Unbounded Promise.all over files is the classic cause.
  • Leaks grow; bursts spike.
  • File watchers cause it in development.

What it means

Each process has a descriptor limit. EMFILE is the error when opening another would exceed it. Then everything needing a descriptor fails, including accepting connections and writing log files.

Common causes

  • Reading thousands of files at the same time.
  • Streams not closed on error.
  • HTTP clients without keep-alive, opening many sockets.
  • Dev tooling watchers on large trees.
  • A low limit imposed by systemd or the container.

Diagnose it

Count open descriptors, and see what kind they are.

bash
# 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 | head

Fixes

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.

Frequently asked questions

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.

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.