Mac OS 上的 NodeJS 错误“EMFILE,打开的文件太多”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19981065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:17:11  来源:igfitidea点击:

NodeJS error "EMFILE, too many open files" on Mac OS

macosnode.js

提问by blagus

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

调用此文件 (mvc.node.js:79) 的代码行是

    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.

如您所见,文件 auth.node.js 由 REQUIRE 调用,因此给定的具有 gracefullFS 和类似功能的解决方案不适合。此外,此问题仅出现在 MacOS 上。在 Ubuntu 中似乎工作得很好。

Any thoughts?

有什么想法吗?

回答by awongh

This worked for me:

这对我有用:

ulimit -n 10480

found here

在这里找到

回答by K M Rakibul Islam

You can solve this problem by increasing the maxfileslimit:

您可以通过增加maxfiles限制来解决此问题:

launchctl limit maxfiles 16384 16384 && ulimit -n 16384

回答by aqm

i had this error and the ulimit and launchclt didn't work for me,

我遇到了这个错误,ulimit 和 launchclt 对我不起作用,

this solution from http://yabfog.com/blog/2014/10/22/yosemite-upgrade-changes-open-file-limitworked for me

这个来自http://yabfog.com/blog/2014/10/22/yosemite-upgrade-changes-open-file-limit 的解决方案对我有用

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

进入 ~/.bashrc

cheers

干杯

Ant

蚂蚁

回答by Ryan Patterson

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.

您的代码打开了太多文件。默认情况下,OS X 限制为 256 个同时打开的文件。当你的代码需要一个新模块时,node 必须打开文件来读入它。如果你已经达到这个限制,node 的 require 将无法继续并抛出错误。您应该审核应用程序中调用 fs.open 的位置,并确保正确关闭所有这些文件。如果您尝试同时进行太多的文件系统读取,您也可能会遇到此问题,因为每个挂起的读取都是一个打开的文件。我在使用 fs.watchFile 时也遇到过这个问题,它也需要打开文件的句柄。

回答by Koray Güclü

Check your ulimit. For example initialy my ulimit on OSX was 256.

检查您的 ulimit。例如,最初我在 OSX 上的 ulimit 是 256。

  • Run ulimit -nto see the limit.
  • Afterwards You can ulimit -n 1024to set a higher limit.
  • 跑去ulimit -n看看极限。
  • 之后您可以ulimit -n 1024设置更高的限制。

回答by bh4r4th

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

我正在使用watchman. 这为我修复了这个错误。值得尝试!!!

brew update
brew install watchman

回答by John Culviner

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:

另请注意,这不会跨会话保存,因此除非您想为每个 bash 终端会话运行它,否则我建议通过执行此操作将上述行放在您的 ~/.bashrc(或 ~/.zshrc,如果您使用 zsh)中在命令行:

vi ~/.bashrc

回答by Squivo

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.

如果您使用终端,则 ulimit 很棒,但它仅在您从同一终端选项卡(或 shell 实例)运行应用程序时才有效。Launchctl 很棒,但适用于整个系统。如果单独保留 Launchctl limit maxfile,则软限制为 256,硬限制为无限制。

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 applicationusing the SoftResourcesLimitkey.

在生产环境中,您可能需要在启动时启动并在崩溃时重新启动,这意味着 Mac OSX 的最佳答案是为每个应用程序使用 .plist 文件。我使用上述 plist 文件(在启动时运行并在崩溃后重新启动)启动我的节点应用程序……在此文件中,您可以使用密钥设置每个应用程序的文件数量SoftResourcesLimit

<key>KeepAlive</key>
<true/>

<key>RunAtLoad</key>
<true/>

<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
  <integer>16384</integer>
</dict>

回答by Pekka

Maximum files was reseted to 256in 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.

256在 OS X 10.10.3 Yosemite 中,最大文件数被重置为。这可能会导致 npm 安装出现问题。您可以使用命令从终端检查此限制ulimit -n。为了将其更改为 Beyond 256,您需要创建两个配置文件。

The first property list file /Library/LaunchDaemons/limit.maxfiles.plist:

第一个属性列表文件/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:

第二个属性列表文件/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 (.bashrcor .bashprofileor similar):

设置为bash的配置文件所需的限制(.bashrc.bashprofile或类似的):

ulimit -n 65536
ulimit -u 2048

Make sure that the rights are the same for bash profile:

确保 bash 配置文件的权限相同:

chmod 644 .your_bash_profile_file

Restart computer and check with ulimit -nmax files. It should be 65536and you should be able to change it anything below that.

重新启动计算机并检查ulimit -n最大文件。它应该是,65536并且您应该能够对其进行任何更改。

Source: http://docs.basho.com/riak/latest/ops/tuning/open-files-limit/#Mac-OS-X

来源:http: //docs.basho.com/riak/latest/ops/tuning/open-files-limit/#Mac-OS-X

回答by withchuck

awongh's answer worked for me

awongh 的回答对我有用

ulimit -n 10480

But only after starting an interactive shell

但只有在启动交互式 shell 之后

sudo -i

Outside of the shell I kept getting a permission error on OSX Yosemite

在外壳之外,我一直在 OSX Yosemite 上收到权限错误