NodeJS error "EMFILE, too many open files" on Mac OS
Macosnode.jsMacos Problem Overview
For sometime I am having the following error:
Error: EMFILE, too many open files '/Users/blagus/Gallery/Websites/Nicsware/Pills/resources/core/auth.node.js'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at Object.Module._extensions..js (module.js:473:44)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at instController (/Users/blagus/Gallery/Websites/Nicsware/Pills/engine/mvc.node.js:79:31)
at init (/Users/blagus/Gallery/Websites/Nicsware/Pills/engine/mvc.node.js:57:8)
at route (/Users/blagus/Gallery/Websites/Nicsware/Pills/engine/dispatcher.node.js:268:36)
The line of code making the call to this file (mvc.node.js:79) is
this.currentRoute.class = require( controllerFile )[dispatchClass].bind( this );
(it is a framework I am creating)
As you can see, the file auth.node.js is called by a REQUIRE, so the given solutions with gracefullFS and similar does not fit. Besides, this problem occour MacOS only. In a Ubuntu seems to work just fine.
Any thoughts?
Macos Solutions
Solution 1 - Macos
Solution 2 - Macos
You can solve this problem by increasing the maxfiles
limit:
launchctl limit maxfiles 16384 16384 && ulimit -n 16384
Solution 3 - Macos
i had this error and the ulimit and launchclt didn't work for me,
this solution from http://yabfog.com/blog/2014/10/22/yosemite-upgrade-changes-open-file-limit worked for me
echo kern.maxfiles=65536 | sudo tee -a /etc/sysctl.conf
echo kern.maxfilesperproc=65536 | sudo tee -a /etc/sysctl.conf
sudo sysctl -w kern.maxfiles=65536
sudo sysctl -w kern.maxfilesperproc=65536
ulimit -n 65536 65536
and then putting the
ulimit -n 65536 65536
into ~/.bashrc
Solution 4 - Macos
I am using watchman
. Which fixed this errors for me. Worth trying!!!
brew update
brew install watchman
Solution 5 - Macos
Your code is opening too many files. By default, OS X has a limit of 256 simultaneously opened files. When your code requires a new module, node has to open the file to read it in. If you are already at this limit, node's require cannot continue and will throw an Error. You should audit places in your application where you are calling fs.open and ensuring that you are properly closing all of those files. You may also encounter this problem if you attempt to do too many simultaneous file system reads, since each pending read will be an open file. I have also encountered this problem while using fs.watchFile, which also requires opening a handle to the file.
Solution 6 - Macos
Check your ulimit. For example initialy my ulimit on OSX was 256.
- Run
ulimit -n
to see the limit. - Afterwards You can
ulimit -n 1024
to set a higher limit.
Solution 7 - Macos
None of the other answers worked for me. This did the trick:
launchctl limit maxfiles 16384 16384
Also to note, this doesn't save across sessions so unless you want to run it for each bash terminal session I suggest putting the above line in your ~/.bashrc (or ~/.zshrc if you are using zsh) by doing this at the command line:
vi ~/.bashrc
Solution 8 - Macos
ulimit is great if you are using the terminal but it only works if you are running your app from the same terminal tab ( or shell instance ). Launchctl is great but is systemwide. If you leave Launchctl limit maxfile alone, the soft limit is 256 and the hard limit is unlimited.
In a production environment, you will probably need to launch at startup and reboot on crash, which means the best answer for Mac OSX is to use a .plist file for each of your applications. I launch my node application using said plist file ( which runs at start up and reboots after crashing )... inside this file you can set the amount of files per application using the SoftResourcesLimit
key.
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>16384</integer>
</dict>
Solution 9 - Macos
Maximum files was reseted to 256
in OS X 10.10.3 Yosemite. This can lead to problems with npm installations. You can check this limit from terminal with command ulimit -n
. In order to change this beyond 256
, you need to create two configuration files.
The first property list file /Library/LaunchDaemons/limit.maxfiles.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>65536</string>
<string>65536</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
The second property list file /Library/LaunchDaemons/limit.maxproc.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>
Set the proper ownership and rights:
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxproc.plist
Set the desired limits to bash profile file (.bashrc
or .bashprofile
or similar):
ulimit -n 65536
ulimit -u 2048
Make sure that the rights are the same for bash profile:
chmod 644 .your_bash_profile_file
Restart computer and check with ulimit -n
max files. It should be 65536
and you should be able to change it anything below that.
> Source: http://docs.basho.com/riak/latest/ops/tuning/open-files-limit/#Mac-OS-X
Solution 10 - Macos
awongh's answer worked for me
ulimit -n 10480
But only after starting an interactive shell
sudo -i
Outside of the shell I kept getting a permission error on OSX Yosemite
Solution 11 - Macos
As for other questions about EMFILE error, you have to manage a queue to limit number of files opened at the same time. Increasing limit will only delay your problem.
There is some responses there : https://stackoverflow.com/questions/8965606/node-and-error-emfile-too-many-open-files
Solution 12 - Macos
You need to reinstall watchman and make sure that brew's version is the one that's gonna be used in the terminal. Just follow these steps in order to do it:
brew update
brew install watchman
brew link --overwrite watchman