Cannot read property 'isDirectory' of undefined

preliminary learning Node, keeps reporting Cannot read property "isDirectory" of undefined, when doing a read about files, judging whether the type of each file is a file directory, and then I try to use isFile, to also report undefined

related codes

function file(i) {
  var filename = filesarry[i];
  fs.stat(__dirname + "/" + filesarry[i], function(err, stat) {
    stats[i] = stat;
    if (stat.isDirectory()) {
      console.log("   " + i + "   \033[36m" + filesarry[i] + "/\033[39m");
    } else {
      console.log("   " + i + "   \033[90m" + filename + "/\033[39m");
    }
    iPP;
    if (i == filesarry.length) {
      read();
    } else {
      file(i);
    }
  });
}

:
C:\Users\DZY\Desktop\Node\\file-explorer\index.js:28
    if (stat.isDirectory()) {
             ^

TypeError: Cannot read property "isDirectory" of undefined
    at C:\Users\DZY\Desktop\Node\\file-explorer\index.js:28:14
    at FSReqWrap.oncomplete (fs.js:152:21)

I have looked up the data everywhere but failed to solve the problem. Beginners of Node, hope to have some advice from the gods who know why

Feb.23,2022

it is possible that an exception was thrown, but you did not catch the exception, so the stat value is undefined.

you can see what the value of err is

.
 fs.stat(__dirname + "/" + filesarry[i], function(err, stat) {
    if (err) {
        throw err
        return false;
    }
    stats[i] = stat;
   // ... 
Menu