Node.js Read Directory

The readdirSync() method is used to read the files and folders inside a directory.

Example

const fs = require("fs");

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

console.log(files);
        
Output:

The files and folders inside the selected directory are displayed.

Explanation

  1. Import the fs module.
  2. Use readdirSync().
  3. Give the directory name.
  4. Store the result in a variable.
  5. Display the directory contents.
← Back to Home