title: node and Error: EMFILE, too many open files
body: <p>For some days I have searched for a working solution to an error</p>
  <blockquote>
  <p>Error: EMFILE, too many open files</p>
  </blockquote>
  <p>It seems that many people have the same problem. The usual answer involves increasing the number of file descriptors. So, I've tried this:</p>
  <pre><code>sysctl -w kern.maxfiles=20480
  </code></pre>
  <p>The default value is 10240. This is a little strange in my eyes, because the number of files I'm handling in the directory is under 10240. Even stranger, I still receive the same error after I've increased the number of file descriptors.</p>
  <p>Second question:</p>
  <p>After a number of searches I found a work around for the &quot;too many open files&quot; problem:</p>
  <pre><code>var requestBatches = {};
  function batchingReadFile(filename, callback) {
    // First check to see if there is already a batch
    if (requestBatches.hasOwnProperty(filename)) {
      requestBatches[filename].push(callback);
      return;
    }
  
    // Otherwise start a new one and make a real request
    var batch = requestBatches[filename] = [callback];
    FS.readFile(filename, onRealRead);
    
    // Flush out the batch on complete
    function onRealRead() {
      delete requestBatches[filename];
      for (var i = 0, l = batch.length; i &lt; l; i++) {
        batch[i].apply(null, arguments);
      }
    }
  }
  
  function printFile(file){
      console.log(file);
  }
  
  dir = &quot;/Users/xaver/Downloads/xaver/xxx/xxx/&quot;
  
  var files = fs.readdirSync(dir);
  
  for (i in files){
      filename = dir + files[i];
      console.log(filename);
      batchingReadFile(filename, printFile);
  </code></pre>
  <p>Unfortunately I still recieve the same error.
  What is wrong with this code?</p>