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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
Using Promises with ES7
Asynchronous use with mz/fs
The
mz
module provides promisified versions of the core node library. Using them is simple. First install the library...Then...
Alternatively you can write them in asynchronous functions in ES7:
Update for recursive listing
Some of the users have specified a desire to see a recursive listing (though not in the question)... Use
fs-promise
. It's a thin wrapper aroundmz
.then...
Here's an asynchronous recursive version.
The answer above does not perform a recursive search into the directory though. Here's what I did for a recursive search (using node-walk:
npm install walk
)Get files in all subdirs