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

后端 未结 25 1291
天涯浪人
天涯浪人 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:26

    non-recursive version

    You don't say you want to do it recursively so I assume you only need direct children of the directory.

    Sample code:

    const fs = require('fs');
    const path = require('path');
    
    fs.readdirSync('your-directory-path')
      .filter((file) => fs.lstatSync(path.join(folder, file)).isFile());
    

提交回复
热议问题