Count the number of Files in a Directory using Node.js

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Count the number of files in a directory using Node.js
  2. Count the number of files in a directory using Async/Await
  3. Count the number of files in a directory Synchronously in Node.js
  4. Count the number of files in a directory Recursively in Node.js

Note: if you need to count the number of files in a directory recursively, click on the following subheading:

# Count the number of files in a directory using Node.js

To count the number of files in a directory:

  1. Use the fs.readdir() method to read the contents of the directory.
  2. Access the length property on the returned array of files to get the number of files in the directory.
index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ if you use the CommonJS require() syntax // const fs = require('fs') const pathToDirectory = './my-folder'; fs.readdir(pathToDirectory, (error, files) => { if (error) { console.log(error); } else { console.log(files); console.log(files.length); } });

count files in directory using node js

The code for this article is available on GitHub

Make sure to specify the correct path to the directory.

The code sample above uses the ES6 modules import/export syntax.

If you use the CommonJS require() syntax, use the following import statement instead.

index.js
const fs = require('fs')

We used the fs.readdir() method to read the contents of the directory.

The method is asynchronous and returns an array of the names of the files in the directory.

# Count the number of files in a directory using Async/Await

The code sample above uses the older callback-style syntax.

In more modern code, you might want to use the async/await syntax.

index.js
import {readdir} from 'fs/promises'; // ๐Ÿ‘‡๏ธ if you use the CommonJS require() syntax // const {readdir} = require('fs/promises') async function countFilesInDirectory(dirPath) { try { const files = await readdir(dirPath); console.log(files); return files.length; } catch (err) { console.log(err); } } const pathToDirectory = './my-folder'; countFilesInDirectory(pathToDirectory).then(count => { console.log(count); });

count files in directory using async await

The code for this article is available on GitHub

Notice that we imported the readdir() method from fs/promises this time.

The async function awaits the Promise that the readdir() method returns and accesses the length property on the array to get the number of files in the directory.

# Count the number of files in a directory Synchronously in Node.js

You can also use the fs.readdirSync() method if you need to count the number of files in a directory synchronously.

index.js
import {readdirSync} from 'fs'; // ๐Ÿ‘‡๏ธ if you use the CommonJS require() syntax // const {readdirSync} = require('fs') function countFilesInDirectory(dirPath) { try { const files = readdirSync(dirPath); console.log(files); return files.length; } catch (err) { console.log(err); } } const pathToDirectory = './my-folder'; const count = countFilesInDirectory(pathToDirectory); console.log(count);
The code for this article is available on GitHub

This time we imported the readdirSync method from the fs module.

The method takes the path to the directory and synchronously reads its contents returning an array that contains the names of the files in the given directory.

The last step is to access the length property on the returned array to get the number of files.

# Count the number of files in a directory Recursively in Node.js

If you need to count the number of files in a directory recursively, use the following code sample instead.

index.js
import {readdirSync, statSync} from 'fs'; import path from 'path'; // ๐Ÿ‘‡๏ธ if you use the CommonJS require() syntax // const {readdirSync, statSync} = require('fs') // const path = require('path') function recursivelyCountFiles(dirPath, files) { const filesCurrentDir = readdirSync(dirPath); let allFiles = files || []; for (const fileName of filesCurrentDir) { const filePath = path.join(dirPath, fileName); if (statSync(filePath).isDirectory()) { allFiles = recursivelyCountFiles(filePath, allFiles); } else { allFiles.push(fileName); } } console.log(allFiles); return allFiles.length; } const pathToDirectory = './my-folder'; const count = recursivelyCountFiles(pathToDirectory); console.log(count);

count number of files in directory recursively

The code for this article is available on GitHub
  1. We used the readdirSync() method to get the names of the files in the given directory.
  2. We used a for...of loop to iterate over the array of file names.
  3. On each iteration, we join the directory name and the filename and check if the path points to a directory.
  4. If the path points to a directory, we invoke the recursivelyCountFiles() function with the directory path and the array of files.
  5. If the path points to a file, we push the filename into the allFiles array.
  6. Lastly, we access the length property of the array of files to get the number of files in the directory recursively.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev