Using the fs module to list a directory in Node.js
Here is how to use the fs module list the files (and directories) in the current directory in Node.js …
var fs = require('fs'); console.log(fs.readdirSync(__dirname)); console.log("Done");
This is a blocking call. Because it is synchronous. It is generally more preferable to do in asynchronous mode as in …
var fs = require('fs'); fs.readdir(__dirname, function(err,files) { console.log(files); }); console.log("Done");
In this case, the “Done” will be printed first before the list of files.