score:0

Accepted answer
const fs = require('fs');
//const folderObject = {files:[]}; //if the parent folder has files
const folderObject = {};
function parseFolder(obj, folderPath){
    const files = fs.readdirSync(folderPath);
    for(file of files){
        const filePath = `${folderPath}/${file}`;
        if(fs.lstatSync(filePath).isDirectory()){
            obj[file] = {files:[]};
            parseFolder(obj[file],filePath);
        } else {
            obj.files.push(file);
        }
    }
}
parseFolder(folderObject,'./node_modules');

Related Query

More Query from same tag