title: NodeJS recursively list files in directory
body: <p>I am trying to list all files in a directory (and files within any subdirectories) with the following code:</p>
  
  <pre><code>var fs = require('fs')
  
  var walk = function(directoryName) {
    fs.readdir(directoryName, function(e, files) {
      files.forEach(function(file) {
        fs.stat(file, function(e, f) {
          if (f.isDirectory()) {
            walk(file)
          } else {
            console.log('- ' + file)
          }
        })
      })
    })
  }
  
  walk(__dirname)
  </code></pre>
  
  <p>However, when my code attempts to invoke <code>walk(file)</code> on line 8 I get the following error:</p>
  
  <pre><code>TypeError: Cannot call method 'isDirectory' of undefined
      at program.js:7:15
      at Object.oncomplete (fs.js:107:15)
  </code></pre>
  
  <p>Why is f undefined? If I have the directory structure below, shouldn't the code identify <code>aaa.txt</code> and <code>bbb.txt</code> as files, <code>my_dir</code> as a directory at which point it recursively calls <code>walk</code> and begins the process again (with <code>zzz.txt</code> being the value of f)?</p>
  
  <pre><code>- aaa.txt
  - bbb.txt
  + my_dir
      - zzz.txt
  </code></pre>