I am trying to list all files in a directory (and files within any subdirectories) with the following 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) However, when my code attempts to invoke walk(file) on line 8 I get the following error:
TypeError: Cannot call method 'isDirectory' of undefined at program.js:7:15 at Object.oncomplete (fs.js:107:15) Why is f undefined? If I have the directory structure below, shouldn't the code identify aaa.txt and bbb.txt as files, my_dir as a directory at which point it recursively calls walk and begins the process again (with zzz.txt being the value of f)?
- aaa.txt - bbb.txt + my_dir - zzz.txt
e, there might have been an error.