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

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

    Use npm list-contents module. It reads the contents and sub-contents of the given directory and returns the list of files' and folders' paths.

    const list = require('list-contents');
    
    list("./dist",(o)=>{
      if(o.error) throw o.error;
       console.log('Folders: ', o.dirs);
       console.log('Files: ', o.files);
    });
    

提交回复
热议问题