-
-
Save quantumsheep/9bf7375c6f71d1ea2d982301b8822731 to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// List all files in a directory in Node.js recursively in an asynchronous fashion | |
const fs = require('fs').promises; | |
const path = require('path'); | |
const walk = async (dir, filelist = []) => { | |
const files = await fs.readdir(dir); | |
for (file of files) { | |
const filepath = path.join(dir, file); | |
const stat = await fs.stat(filepath); | |
if (stat.isDirectory()) { | |
filelist = await walk(filepath, filelist); | |
} else { | |
filelist.push(file); | |
} | |
} | |
return filelist; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment