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

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Import the filesystem module
const fs = require("fs");
  
let directory_name = "example_dir";
  
// Function to get current filenames
// in directory
let filenames = fs.readdirSync(directory_name);
  
console.log("\nFilenames in directory:");
filenames.forEach((file) => {
    console.log("File:", file);
});

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Import the filesystem module
const fs = require("fs");
  
let directory_name = "example_dir";
  
// Open the directory
let openedDir = fs.opendirSync(directory_name);
  
// Print the pathname of the directory
console.log("\nPath of the directory:", openedDir.path);
  
// Get the files present in the directory
console.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;
}

chevron_right


Output:

Path of the directory: example_dir
Files Present in directory:
Name: file_a.txt
Name: file_b.txt
Name: file_c.tx



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.