62

有时我遇到以下错误:

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)

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

    this.currentRoute.class = require( controllerFile )[dispatchClass].bind( this );

(这是我正在创建的框架)

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

有什么想法吗?

4

11 回答 11

58

这对我有用:

ulimit -n 10480

在这里找到

于 2014-01-18T07:35:29.777 回答
48

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

launchctl limit maxfiles 16384 16384 && ulimit -n 16384
于 2014-08-11T05:41:03.473 回答
22

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

这个来自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

然后把

ulimit -n 65536 65536

进入~/.bashrc

于 2015-01-16T10:45:18.110 回答
13

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

brew update
brew install watchman
于 2018-08-15T06:40:47.090 回答
9

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

于 2013-11-22T21:44:06.517 回答
7

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

  • 跑来ulimit -n看看极限。
  • 之后您可以ulimit -n 1024设置更高的限制。
于 2015-10-06T08:25:54.770 回答
6

其他答案都不适合我。这成功了:

launchctl limit maxfiles 16384 16384 

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

vi ~/.bashrc
于 2014-10-22T16:59:58.610 回答
2

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

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

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

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

<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
  <integer>16384</integer>
</dict>
于 2014-11-26T17:04:47.453 回答
1

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

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

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

设置适当的所有权和权利:

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

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

ulimit -n 65536
ulimit -u 2048

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

chmod 644 .your_bash_profile_file

重新启动计算机并检查ulimit -n最大文件。它应该是65536,你应该能够改变它低于它的任何东西。

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

于 2015-09-24T17:19:02.837 回答
0

awongh的回答对我有用

ulimit -n 10480

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

sudo -i

在外壳之外,我在 OSX Yosemite 上不断收到权限错误

于 2016-06-02T19:12:33.800 回答
0

至于其他关于 EMFILE 错误的问题,您必须管理一个队列以限制同时打开的文件数量。增加限制只会延迟您的问题。

那里有一些响应:节点和错误:EMFILE,打开的文件太多

于 2018-07-31T09:27:36.130 回答