I'm trying to make a file indexer by node.js. The program is supposed to index files and folders in an array and also check inside folders and add all sub folders and filesI wrote:fs.readdir(__dirname,index);function index(err,data){ for (i in data){        if(fs.statSync(data[i]).isDirectory()){            j.folders.push(path.resolve(data[i]));            fs.readdir(data[i],index);        }        else{            j.files.push(path.resolve(data[i]));        }    } } ... this code, but when it passes inside a folder it gives traceback and stops executing.here my question is how I index files and solve this issue.ps. please don't comment about walk, I want to write my own module.
node deep file indexer module cannot go deep
javascript;node.js;io;indexing;directory structure
An issue with readdir is that it passes in only the filename, without the full path, to the data variable in your index function.  Let's say you are calling the script from C:\main.  The contents of C:\main areC:\mainC:\main\folder1C:\main\folder1\subDir1 Once your program enters folder1 and reads subDir1, it will try to call readdir with data[i] = 'subDir1'.  But since the current working directory hasn't changed, which is still C:\main, readdir joins this to its path argument, resulting in C:\main\subDir1, which doesn't exist.To handle this, we can track the directory that was sent in to readdir, which is asynchronous.  This means that the directory needs to be defined in the callback function before it is invoked.var fs = require('fs');var path = require('path');var files = [];var folders = [];indexDir(__dirname);function indexDir(d) {fs.readdir(d, function (err,data){ for (i in data){        console.log(path.join(d, data[i]));        if(fs.statSync(path.join(d, data[i])).isDirectory()){            folders.push(path.resolve(path.join(d, data[i])));            indexDir(path.join(d, data[i]));        }        else{            files.push(path.resolve(path.join(d, data[i])));        }    }})}If we want to see the results once the directory has been completely traversed, then it might be easier to just use readdirSync, since it is synchronous and will block until all the directories have been visited.  Otherwise, if we want to keep things asynchronous, we can call another function when everything is done by keeping track of the number of asynchronous calls.var fs = require('fs');var path = require('path');var files = [];var folders = [];var calls = 0;indexDir(__dirname);function indexDir(d) { calls++; fs.readdir(d, function (err,data){   calls--;   for (i in data){          if(fs.statSync(path.join(d, data[i])).isDirectory()){              folders.push(path.resolve(path.join(d, data[i])));              indexDir(path.join(d, data[i]));          }          else{              files.push(path.resolve(path.join(d, data[i])));          }      }   if (calls == 0){     present();   } })}function present(){ for (i in files){        console.log(files[i]);    } for (i in folders){        console.log(folders[i]);    } }