Skip to content

Instantly share code, notes, and snippets.

@tspringborg
Last active July 7, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspringborg/c23fc6c379da1256a432a0f18446baff to your computer and use it in GitHub Desktop.
Save tspringborg/c23fc6c379da1256a432a0f18446baff to your computer and use it in GitHub Desktop.
synchronously recursively list directory files in nodejs with fs
const fs = require('fs');
const walk = function (dir) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
results.push(file);
}
});
return results;
};
module.exports = walk;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment