I am trying to create a module to return a list of files in directory using fs. The console.log returned array of files as expected but in the end returned object is undefined. Can somebody explain this to me.
the module:
exports.fileList = function () {
var fs = require('fs');
var path = require('path');
var dirPath = 'C:/Users/Desktop/Hotkey'; //directory path
var files = [];
fs.readdir(dirPath, function(err,list){
if(err) throw err;
for(var i=0; i<list.length; i++){
console.log(i + ':' + list[i]); //print the file
files.push(list[i]); //store the file name into the array files
}
console.log(files); // array here is displayed correctly
});
return files; //return undefined object
}