Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursively, synchronously, read files from a directory with nodejs.
var recursiveReaddirSync = function (topDir) {
function readdir (dir) {
fs.readdirSync(dir).filter(function (pth) {
return !/^\./.test(pth)
}).forEach(function (pth) {
pth = path.join(dir, pth)
if (fs.lstatSync(pth).isDirectory()) readdir(pth)
else files.push(pth)
})
}
var files = []
readdir(topDir)
return files
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment