fs to fs-extra using node js I wrote a code by using var fs= require(&quot;fs&quot;).promises;

to read all my files in directories and sub-directories



var fs= require("fs").promises;

async function checkFileLoc(folderPath, depth) {
  depth -= 1;
  let files = await fs.readdir(folderPath);
  files = await Promise.all(
    files.map(async (file) =&gt; {
      const filePath = path.join(folderPath, file);
      const stats = await fs.stat(filePath);
      if (stats.isDirectory() &amp;&amp; depth &gt; 0) {
        return checkFileLoc(filePath, depth);
      } else if (stats.isFile()) return filePath;
      else return null;
    })
  );
  return files
    .reduce((all, folderContents) =&gt; all.concat(folderContents), [])
    .filter((e) =&gt; e != null);
}



Then I install fs-extra package in my project and replace var fs=require(&quot;fs&quot;);to var fse=require(&quot;fs-extra&quot;); and made changes in my code like this


var fse=require("fs-extra");

async function checkFileLoc(folderPath, depth) {
  depth -= 1;
  let files = await fse.readdir(folderPath);
  files = await Promise.all(
    files.map(async (file) =&gt; {
      const filePath = path.join(folderPath, file);
      const stats = await fse.stat(filePath);
      if (stats.isDirectory() &amp;&amp; depth &gt; 0) {
        return checkFileLoc(filePath, depth);
      } else if (stats.isFile()) return filePath;
      else return null;
    })
  );
  return files
    .reduce((all, folderContents) =&gt; all.concat(folderContents), [])
    .filter((e) =&gt; e != null);
}



when I was using fs i was getting the desire output and someone told me fs-extra is advance then fs and all you have to do is replace fs with fse
my code not working properly any solution where i have made mistake ?
I tried to figure it out what changes i have to made if I am using var fse= require(&quot;fs-extra&quot;);
so my code will look like this


var fse=require("fs-extra");

async function checkFileLoc(folderPath, depth) {
  depth -= 1;
  let files = fse.readdirSync(folderPath);
  files = await Promise.all(
    files.map(async (file) =&gt; {
      const filePath = path.join(folderPath, file);
      const stats = fse.statSync(filePath);
      if (stats.isDirectory() &amp;&amp; depth &gt; 0) {
        return checkFileLoc(filePath, depth);
      } else if (stats.isFile()) return filePath;
      else return null;
    })
  );
  return files
    .reduce((all, folderContents) =&gt; all.concat(folderContents), [])
    .filter((e) =&gt; e != null);
}



I replaced my

var fs=require(&quot;fs&quot;); to var fse=require(&quot;fs-extra&quot;);
let files = await fs.readdir(folderPath); with let files = fs.readdirSync(folderPath);
const stats = await fs.stat(filePath); with const stats = fs.statSync(filePath);

now i am getting my desire output