title: Are closures different in node.js?
body: <p>I have worked quite a lot with javascript but yesterday, I started using node.js. It's a little script that runs jslint on the files of a folder. For this example, I changed the command to call <code>ls</code> instead of <code>jslint</code>.</p>
  <pre><code>var sys = require(&quot;sys&quot;);
  var fs = require(&quot;fs&quot;);
  var cp = require('child_process');
  
  var path = fs.realpathSync(&quot;./src/&quot;);
  
  fs.readdir(fs.realpathSync(&quot;./src/&quot;), function(err, files) {
    for (var i = 0; i &lt; files.length; i++) {
      var filename = files[i];
      var complete = path + filename;
  
      // Run jslint on each file
      var jslint = cp.exec(&quot;ls &quot; + complete, function(error, stdout, stderr) {
        console.log(filename + &quot; : &quot; + stdout);
      });    
    }
  });
  </code></pre>
  <p>The output is this :</p>
  <blockquote>
  <p>jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.undo.js</p>
  <p>jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.nofreeze.js</p>
  <p>jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.timezone.js</p>
  </blockquote>
  <p>Why do the line <code>console.log(filename + &quot; : &quot; + stdout);</code> always prints <code>jskata.nofreeze.js</code> when the filename should obviously match the result of the <code>ls</code>? Are closures and scopes different in node.js than in javascript?</p>