[node.js] How to get all directories within directory

To check whether a directory exists, you must use the isDirectory() function of the returned Dirent object.

var fs = require('fs');

fs.readdir('./backup', { withFileTypes: true }, function(err, folders){

    console.log('all counts: '+folders.length);
    let counter = 0;

    folders.forEach(folder=>{

        if(folder.isDirectory()){ // check directory

            counter++;
            console.log(folder.name);
        }
    })
});

To check if it is a file, use the isFile() function instead of isDirectory().