How do you get a list of the names of all files present in a directory in Node.js?

后端 未结 25 1714
天涯浪人
天涯浪人 2020-11-22 07:47

I\'m trying to get a list of the names of all the files present in a directory using Node.js. I want output that is an array of filenames. How can I do this?

25条回答
  •  误落风尘
    2020-11-22 08:33

    if someone still search for this, i do this:

    import fs from 'fs';
    import path from 'path';
    
    const getAllFiles = dir =>
        fs.readdirSync(dir).reduce((files, file) => {
            const name = path.join(dir, file);
            const isDirectory = fs.statSync(name).isDirectory();
            return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
        }, []);

    and its work very good for me

提交回复
热议问题