"connect EMFILE" error in Node.js
Solution 1:
EMFILE
error means that the OS is denying your program to open more files/sockets.
Have a look at: How do I change the number of open files limit in Linux?
Solution 2:
EMFILE
means error maximum files
which indicates that the OS is denying your program to open more file descriptors.
Note that open files in the system include disk files, named pipes, network sockets and devices opened by all processes.
Your operating system specifies the open file limit per process.
To check open file limit of your system use ulimit -a
command. Generally on unix like system it is default to 1024.
If the limitation was 1024, means you/process can open maximum 1024 files. if you exceed this limit means open
, pipe
and dup
system calls will fail and yield EMFILE error.
When you get EMFILE
it mostly indicates the leak in your code. Increasing the ulimit to a higher value is not a good solution in case there is leak in your program.
You should try to find out the cause of leak. Following can be used to debug in unix like operating system :
-
lsof
meaninglist open files
command gives the list of open files. Assume your nodeJS program is having leak so to find out total number of file descriptors opened by your program :lsof | grep node | wc -l
-
To find out the file descriptors for sockets, use:
lsof -n -i -P | grep node
Using this we can find out where the leak is and then we can correct it.
Solution 3:
In my case, we already had the ulimit set and kern.maxfiles configuration highest it could go on a Mac.
What fixed it was limiting the number of max sockets globally. In later versions of node the limit is Infinity.
Try adding these lines of code:
var https = require('https');
var http = require('http');
https.globalAgent.maxSockets = 5;
http.globalAgent.maxSockets = 5;
In the case of using the request library, you can configure these settings.
See: https://github.com/request/request#request---simplified-http-client
Here is more on maxSockets: https://nodejs.org/api/http.html#http_agent_maxsockets