How do you recursively get a list of all the files in a directory using Node.js?

Node.js has a built-in Fs core module that provides an fs.readdirSync() function that reads the contents of a directory at a given file path.

In this article, we'll show you both a basic example of how to use the fs.readdirSync() function and also how to use it in a more advanced way to recursively go through each subdirectory and get each of their files as well.

To get the code in this article to work, make sure you have Node.js installed on your local machine.

If you need one, we created a guide on installing Node.js.

Let's get started!

Table of Contents

Basic Usage of fs.readdirSync()

Before we create the recursive solution, let's go over a simple example using the fs.readdirSync() function.

The fs.readdirSync() function takes a directory path and a callback function for arguments. And it returns an array of the files as a relative path to the directory you passed as a parameter.

Here's what it looks like in practice, assuming you have a directory named "my-directory" with files in it:

      const fs = require("fs")

      try {
        const arrayOfFiles = fs.readdirSync("./my-directory")
      } catch(e) {

Let's go over what's happening in the code.

First of all, we require() the fs module. Since it's a core module, we don't need to install anything and can use it right away.

Since fs.readdirSync() is a synchronous function, we wrap our code in a try...catch statement to help manage our code and catch any errors that might occur.

Inside the try section, we pass a path to a directory named "my-directory" to the fs.readdirSync() function and hold it in a variable called arrayOfFiles.

When that variable is logged, you'll have an array of relative file paths to each file and subdirectory inside the directory.

The output will look similar to this:

      ["example.txt", "sub-directory"]

What if our directory has sub-directories that have files we want to include in our list?

In the next section, we'll show you how to get those as well.

Get All the Files in the Directory

In this section, we'll show you how to recursively get all the files in a directory (even those located in a subdirectory).

To do this, we need to create a recursive function that can call itself when dealing with sub-directories. And we also need the function to go through each of the sub-directories and add any new files it encounters. When the function is finished, it should return an array with all the files it encountered.

Here's what the recursive function looks like:

      const fs = require("fs")
      const path = require("path")

      const getAllFiles = function(dirPath, arrayOfFiles) {
        files = fs.readdirSync(dirPath)

        arrayOfFiles = arrayOfFiles || []

        files.forEach(function(file) {
          if (fs.statSync(dirPath + "/" + file).isDirectory()) {
            arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
          } else {
            arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))

        return arrayOfFiles

Let's go over each part of the code.

First, we require() the Node.js path module. Since this is included with Node.js, you don't need to install anything for it to work. This module will help us easily create full file paths for our files.

The getAllFiles variable holds the recursive function that will go through each subdirectory and return an array of filenames. It takes a directory file path and an optional arrayOfFiles as arguments.

Inside the getAllFiles function, we first use the readdirSync() function to get all of the files and directories inside the given dirPath supplied to the function.

Then, we create an arrayOfFiles that will hold all the filenames that will be returned when the function is done running.

Next, we loop over each item (file or directory) found by the readdirSync() function. If the item is a directory, we have the function recursively call itself to get all of the files and sub-directories inside the given directory.

And if the item is a file, we simply append the file path to the arrayOfFiles array.

When the forEach loop has finished, we return the arrayOfFiles array.

Here is how you use the function in your code:

      const result = getAllFiles("PATH_TO_PARENT_DIRECTORY")
      // [ "FILE_PATH", "FILE_PATH", "FILE_PATH" ]

There you have it! When you call the getAllFiles function, it'll return a list of all the files.

Pretty sweet, right?


In this article, you learned how to get a list of all the files in a directory using Node.js, including all of its sub-directories. And implemented a recursive function to get the job done.

Thanks for reading and happy coding!