Node.js crashes with EMFILE: too many open files
Error: 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.
Fix it fast
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 -l2Match what you see, then jump to its fix
- 1
ulimit -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
The command works in your terminal but the service logs EMFILEA systemd service runs with its own limitFix - 3
EMFILE 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
- 1
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 -n2Fix
Raise the limit in the same shell that runs Node.js, as the docs advise.
ulimit -n 2048
node build.js3Verify
The new limit shows, and the job finishes.
ulimit -nConfirmed by nodejs.org
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-app3Verify
The running process reports the new limit.
cat /proc/$(systemctl show my-app -p MainPID --value)/limits | grep "open files"Confirmed by freedesktop.org
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/ | head2Fix
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 -lConfirmed by github.com
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
Back end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fixBack end
fluent-ffmpeg cannot find the ffmpeg or ffprobe binary
Error: Cannot find ffprobe
fluent-ffmpeg does not bundle ffmpeg.
4 causes, each with a fix