FS Module

Let's experiment with the File system (FS) module.

// index.js file
const fs = require("fs");

const files = fs.readdirSync("./");

console.log(files);

The readdirSync will synchronously read the files stored in the given directory (the path to the directory is given as an argument to readdirSync function).

On my computer, running index.js results in:

[ 'account.js', 'index.js' ]

There is an asynchronous version of this function which you can (and should) use:

// index.js file
const fs = require("fs");

fs.readdir("./", (err, files) => {
  console.log(files);
});

Notice we have used the callback pattern to deal with the async operation. The readdir function was written before JavaScript had Promise object. The function does not return a Promise so we cannot use the Promise pattern (nor async/await) here.