var dir = "<path>";
var dir1 = new Array();
var files1 = new Array();
dir1 = [];
files1 = [];
function file_tree(dir1, files1) {
  const filesArray = fs.readdirSync(dir).filter(file => fs.lstatSync(dir + file))
  for (let i = 0; i < filesArray.length; i++) {
    // Use stat() method
    fs.stat(dir + filesArray[i], (err, stats) => {
      if (!err) {
        if (stats.isDirectory()) {
          dir1.push(filesArray[i]);
        } else if (stats.isFile()) {
          files1.push(filesArray[i]);
        }
      } else
        throw err;
    });
  }
  console.log(dir1, files1, "return")
  return dir1, files1
}
dir1, files1 = file_tree(dir1, files1)
console.log(dir1)
console.log(files1);
Output:
[] [] return
[]
[]
In my opinion the problem is that the push statement doesn't work globally and I need to run it globally. but I do not know how!?
(If I put console.log(files1), in the if statement, after "push", then the file or dir list will be output)
Goal: That in fils1 all files of the dir (in which I am) are listed and in dir1 all dir of the dir (in which I am) are listed
 
    