nodejs fs list files & directory
nodejs fs list files & directory

Hi Guy’s Welcome to Proto Coders Point. In this NodeJS Article let’s learn How to list folders of a working directory using file system (fs) library.

Node list all files & folders in directory

I will assume you already have a node project opened or created.

As I said to work with File System like creating folder, reading files from a folder we require NodeJS fs library.

Install fs module using below command

node install fs

Read directory using fs module nodejs

In NodeJS, As I said we must make using of file system module to work with system files using nodejs. In fs library we have a function i.e. readdir() using which we can list out all the files & folders/directory of a working directory.

The fs.readdir() function take two arguments:- first: the path from where you want to read files & folders and second: is a callback function that will hold the contents of a directory.


Example 1 : List all the Files & Folders from a given directory path

below example will print the name of files & folders of a current directory

const fs = require('fs');

const directoryPath = './your-directory';

fs.readdir(directoryPath, function (err, files) {
  if (err) {
    return console.log('Unable to read directory: ' + err);
  } 
  console.log(files);
});

Note: you need to replace your-directory path with the path of a directory that you want read out and list files & directory.


Example 2 : List only Folders/Directory from a given directory path

In below code, I am using fs.readdir() to read all the files & directory in a given directory, and then to list out only Folders/Directory by iterating to each files or folder using filter, during filtering I am checking if the file is a directory or a file by using fs.statSync(path).isDirectory(); If the file is directory then adding it to the list.

//joining path of directory 
    const directoryPath = path.join('myRootFolder');
    
    //passsing directoryPath and callback function
    fs.readdir(directoryPath, (err, files) => {
        if (err) {
            console.error(err);
            return;
        }
         // Here, only directory will get filtered
        const directories = files.filter(file => {
            const filePath = path.join(directoryPath, file);
            return fs.statSync(filePath).isDirectory();
        });

        console.log(directories);
    });

Example 3 : List only files from a given directory path

In below code, I am using fs.readdir() to read all the files & directory in a given directory, and then to list out only files by iterating to each files or folder using filter, during filtering I am checking if the file is a files or a directory by using fs.statSync(path).isFile(); If the file is a file then adding it to the list.

//joining path of directory 
    const directoryPath = path.join('myRootFolder');
    
    //passsing directoryPath and callback function
    fs.readdir(directoryPath, (err, files) => {
        if (err) {
            console.error(err);
            return;
        }
         // Here, only files will get filtered
        const directories = files.filter(file => {
            const filePath = path.join(directoryPath, file);
            return fs.statSync(filePath).isFile();
        });

        console.log(directories);
    });