How to display all files in a directory using Node.js ?

21 May, 2020
The files present in a directory can be displayed using two approaches in Node.js that are discussed below:
Method 1: Using fs.readdirSync() method: The fs.readdirSync() is a method that is available in the file system module of Node.js. It is used for reading the contents of a directory. It returns an array of file paths, buffers, or fs.Dirent objects.
The returned array of files can be looped through using the forEach() loop and the file names can be displayed from it. This allows one to display all the filenames present in the directory.
The asynchronous variation of the method fs.readdir() can also be used in place of this function that returns a callback with the files instead of the files themselves.
Example:
// Import the filesystem moduleconst fs = require("fs");  let directory_name = "example_dir";  // Function to get current filenames// in directorylet filenames = fs.readdirSync(directory_name);  console.log("\nFilenames in directory:");filenames.forEach((file) => {    console.log("File:", file);});
Output:
Filenames in directory:
File: file_a.txt
File: file_b.txt
File: file_c.txt

Method 2: Using the fs.opendirSync() method: The fs.opendirSync() method is available in the file system module of Node.js. It can be used to read the contents of a directory. It returns an fs.Dir object that represents the given directory.
The fs.Dir object can be used to access the files in that directory using the readSync() method. This method returns an fs.Dirent object that contains the representation of the directory entry. This object has a name property that can be used to get the file name that this fs.Dirent object refers to. This name can be accessed and displayed to this user. The readSync() method will automatically read the next directory entry and return null when no more entries exist. This can be used to continuously loop through the entries and get all the file names using a while loop.
The asynchronous variation of the method fs.opendir() can also be used in place of this function that returns a callback with the fs.Dir object instead of the object itself.
Example:
// Import the filesystem moduleconst fs = require("fs");  let directory_name = "example_dir";  // Open the directorylet openedDir = fs.opendirSync(directory_name);  // Print the pathname of the directoryconsole.log("\nPath of the directory:", openedDir.path);  // Get the files present in the directoryconsole.log("Files Present in directory:");  let filesLeft = true;while (filesLeft) {  // Read a file as fs.Dirent object  let fileDirent = openedDir.readSync();    // If readSync() does not return null  // print its filename  if (fileDirent != null)    console.log("Name:", fileDirent.name);    // If the readSync() returns null  // stop the loop  else filesLeft = false;}
Output:
Path of the directory: example_dir
Files Present in directory:
Name: file_a.txt
Name: file_b.txt
Name: file_c.tx

Node.js-Misc
Picked
Node.js
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
JWT Authentication with Node.js
Installation of Node.js on Windows
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React