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
- Import the fs module.
- Use readdirSync().
- Give the directory name.
- Store the result in a variable.
- Display the directory contents.
↠Back to Home