Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js crashes with EMFILE: too many open files

ErrorError: EMFILE: too many open files, open '/path/to/file'

Short answer

EMFILE means your Node.js process hit its limit of open file descriptors. Raise the limit in the shell or service that starts Node.js, and stop opening thousands of files in parallel. Reading a directory tree with an unbounded Promise.all is the usual trigger, and macOS has a low default.

  • Applies to Node.js 22, Node.js 24, macOS 15, Ubuntu 24.04, systemd 255
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the open-file limit of the shell that starts Node.js, then compare it with how many descriptors the process holds.

    ulimit -n
    lsof -p $(pgrep -n node) | wc -l
  2. 2Match what you see, then jump to its fix

    1. 1ulimit -n prints a small number such as 256 and the error appears during a build or a test runThe per-process limit is low (common on macOS)Fix
    2. 2The command works in your terminal but the service logs EMFILEA systemd service runs with its own limitFix
    3. 3EMFILE appears even with a high limit, on a path that maps over thousands of files with Promise.allThe code opens too many files at onceFix

What does the error look like?

  • Error: EMFILE: too many open files, open '/path/to/file'
  • code: 'EMFILE'

Cause 1 of 3

The per-process limit is low (common on macOS)

1Check

The Node.js docs say EMFILE shows up when many files are opened in parallel, especially on systems like macOS with a low descriptor limit for processes.

ulimit -n

2Fix

Raise the limit in the same shell that runs Node.js, as the docs advise.

ulimit -n 2048
node build.js

3Verify

The new limit shows, and the job finishes.

ulimit -n

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

A systemd service runs with its own limit

1Check

A service does not inherit your shell's ulimit. systemd sets it per unit with LimitNOFILE=, the same setting as ulimit -n. Read the effective value of the running service.

systemctl show my-app -p LimitNOFILE
cat /proc/$(systemctl show my-app -p MainPID --value)/limits | grep "open files"

2Fix

Set LimitNOFILE in a drop-in file, then restart. Pick a number that fits your real load; do not go to extremes.

sudo systemctl edit my-app
# in the editor:
[Service]
LimitNOFILE=65536
# then
sudo systemctl restart my-app

3Verify

The running process reports the new limit.

cat /proc/$(systemctl show my-app -p MainPID --value)/limits | grep "open files"

Confirmed by freedesktop.org

Did this fix it?

Cause 3 of 3

The code opens too many files at once

1Check

Each pending fs call can hold a descriptor. Look for unbounded parallel reads, writes or directory walks in your own code and in file-watching tools.

grep -rn "Promise.all" src/ | head

2Fix

Use graceful-fs, which queues open and readdir calls and retries them after EMFILE. Patch fs only at the top of your application, never in a library. It cannot handle EMFILE from sync methods, so also cap your own concurrency.

npm install graceful-fs

// top of the app entry file
const realFs = require('fs');
const gracefulFs = require('graceful-fs');
gracefulFs.gracefulify(realFs);

3Verify

Run the heavy job again and watch the descriptor count stay under the limit.

lsof -p $(pgrep -n node) | wc -l

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Process big file lists in batches instead of one Promise.all over everything.
  • Close streams and file handles in a finally block.
  • Set LimitNOFILE in every unit file of a Node.js service.
  • Watch the descriptor count in monitoring so a leak shows before a crash.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.