69

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?

asked Nov 14, 2013 at 15:03
3
  • Is auth.node.js doing something strange? If I generate a stacktrace when I require a JS file, fs.* doesn't show up. Commented Nov 14, 2013 at 16:10
  • Well, at a Linux box it behaves just fine. Besides is just a simple require that give me trouble Commented Nov 19, 2013 at 22:06
  • Looking at the stack trace, the problem isn't caused by the require itself but by the module that you're requiring. Then again, if you don't post code it's just a guess... Commented Nov 20, 2013 at 6:16

15 Answers 15

61

This worked for me:

ulimit -n 10480

found here

answered Jan 18, 2014 at 7:35
Sign up to request clarification or add additional context in comments.

2 Comments

as Ryan mentioned below. the default limit is 256. setting it to 10480 is probably overkill. Bump it up incrementally ulimit -n 360
Thanks Lane as well :) anyone know how to kill the processes or close the opened files ?
49

You can solve this problem by increasing the maxfiles limit:

launchctl limit maxfiles 16384 16384 && ulimit -n 16384
answered Aug 11, 2014 at 5:41

9 Comments

I won't downvote as this answer is technically correct, but also very dangerous, especially as since Lion the default amount of simultaneous files is along the 10000 mark, setting the limit this low on a running machine can render all open programs unusable in a second. Let's suggest 16384 instead of 2048 instead if we're trying to bump UP the amount
Well, this fixed the issue for me, no more Error: EMFILE, too many open files ! Thank you. Also, not sure of what I'm doing, but it works. I would be glad to know the real issue behind this problem, why does node pops out this error message. Thank you anyway.
I agree w Igor; I am not sure increasing the limit is an acceptable answer for some as this a security risk. I think in reality at some point gulp needs to "let go" of the stream of files. One way to do this is chain some of your gulp tasks via shell scripting . Yes this seems odd but what is the alternative in gulp? She'll or npm tasks/scripts via npm run.
You have to put sudo at the beginning. It didn't work until I added sudo
This did not seem to do anything for me.
|
23

I am using watchman. Which fixed this error for me. Worth trying!!!

brew update
brew install watchman

It watches the files and documents and triggers file handling events if there are any changes. Here's their github repo:

JimmyT
7796 silver badges7 bronze badges
answered Aug 15, 2018 at 6:40

8 Comments

This is the only one that worked for me, on Catalina 10.15.1.
Catalina 10.15.3 here as well - only solution that worked
Can you provide some context as to how this works? Posting two installation commands is not helpful.
Hi @Contrapak As the name says, it just watches the files and documents. Triggers file handling events if there are any changes. Please look into their github repo for better understanding. github.com/facebook/watchman
@saylestyler I never turned auto updates for homebrew. But I manually run brew update && brew upgrade which updates and upgrades everything including homebrew. Never worried about what version of watchman is running TBH.
|
22

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

NearHuscarl
83.1k24 gold badges321 silver badges291 bronze badges
answered Jan 16, 2015 at 10:45

1 Comment

should mention it's a hacky solution but one to helps out when your local machine isn't working well
8

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.

answered Nov 22, 2013 at 21:44

1 Comment

Do you have any resources to make sure I'm doing what you mentioned? I would prefer to practice better usage of fs instead of just having to keep upping the limit as others have suggested.
7

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.
answered Oct 6, 2015 at 8:25

Comments

6

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
RedYeti
1,28416 silver badges29 bronze badges
answered Oct 22, 2014 at 16:59

Comments

2

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>
RedYeti
1,28416 silver badges29 bronze badges
answered Nov 26, 2014 at 17:04

Comments

1

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

answered Sep 24, 2015 at 17:19

Comments

1

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

answered Jun 2, 2016 at 19:12

Comments

1

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
Skatox
4,27412 gold badges47 silver badges50 bronze badges
answered Apr 12, 2022 at 16:21

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

I solved this problem for me by downgrading node.js

I did

nvm install 18.7.0

I then reinstalled my node_modules using

rm -rf node_modules
rm package-lock.json 
npm install

I do not know why this solved the problem. I imagine that there was an incompatibility between the node.js (version 20) and xCode on the Mac (v. 15.2)

answered Jul 24, 2024 at 13:17

Comments

0

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 : node and Error: EMFILE, too many open files

answered Jul 31, 2018 at 9:27

Comments

0

In my case (Mac M1, macOS Sonoma, React Native project) solution was: using watchman with architecture not ARM but x86_64

arch -x86_64 /usr/local/bin/brew install watchman

in order to do this need first to install brew with architecture x86_64

x86_64 version brew is installed in /usr/local folder whereas ARM version brew is installed in /opt/homebrew/

useful comment

answered Apr 5, 2024 at 2:33

Comments

-3

None of the above solutions work on Mac M1 chip. If you face above issue and can't make it work - the solution is to go to eBay and buy used Intel based MacBook - can get one for around 300ドル.

answered Sep 12, 2023 at 13:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.