Node.js で Error: ENFILE: file table overflow, open xxx が発生したときの解決方法をご紹介します。
 
Error: ENFILE: file table overflow, open xxx はファイルディスクリプタ数の上限を超えたときに発生するエラーです。
なので、解決方法はファイルディスクリプタ数の上限を増やすだけでよいです。
今回は MacOS でエラーが発生したので、Macのファイルディスクリプタ上限を上げる - 橋本商会 を参考にして対応しました。
$ ulimit -a
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       709
-n: file descriptors                256
/Library/LaunchDaemons/ に以下の2ファイルを追加・更新します。
limit.maxfiles.plist
  
  
    Label 
    limit.maxfiles 
    ProgramArguments 
    
      launchctl 
      limit 
      maxfiles 
      524288 
      524288 
     
    RunAtLoad 
    
    ServiceIPC 
    
     
 
limit.maxproc.plist
  
  
    Label 
      limit.maxproc 
    ProgramArguments 
      
        launchctl 
        limit 
        maxproc 
        2048 
        2048 
       
    RunAtLoad 
      
    ServiceIPC 
      
     
 
追加した plist の所有権を root にする。
sudo chown root /Library/LaunchDaemons/limit.max*.plist
plist の設定を有効にする。
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxproc.plist
ここで、OS を再起動します。
ulimit -a  
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       2048
-n: file descriptors                524288
以上、突然の Error: ENFILE: file table overflow, open xxx に悩まされた現場からお送りしました。