Next.js starter your AI actually understands. Ship internal tools in days not weeks. Pre-order $199 $499 → [Get it now]

How to read directories in Node.js

Reading directory contents is essential for file system operations, building file explorers, and processing multiple files in Node.js applications. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented directory reading functionality in numerous backend services over 25 years of development. From my expertise, the most straightforward approach is using the fs.readdir() method, which returns an array of filenames and subdirectories. This provides the foundation for file system navigation and batch file processing.

Use fs.readdir() to get an array of files and directories in a folder.

const fs = require('fs')

fs.readdir('./uploads', (err, files) => {
  if (err) throw err
  console.log(files) // ['file1.txt', 'file2.jpg', 'subfolder']
})

Here fs.readdir() takes a directory path and a callback function that receives an error object and an array of filenames. The files array contains both files and subdirectories as strings. Use this data to iterate through directory contents, filter by file types, or perform batch operations on multiple files.

Best Practice Note:

This is the same directory reading approach we use in CoreUI build tools and file processing utilities. For recursive directory traversal or detailed file information, consider using fs.readdir() with { withFileTypes: true } option or the promise-based fs.promises.readdir() for cleaner async code.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to replace all occurrences of a string in JavaScript?
How to replace all occurrences of a string in JavaScript?

How to Hide Scrollbar with CSS
How to Hide Scrollbar with CSS

8 Best Free Bootstrap Admin Templates for 2026 - Comparison & Reviews
8 Best Free Bootstrap Admin Templates for 2026 - Comparison & Reviews

How to Center a Button in CSS
How to Center a Button in CSS

Answers by CoreUI Core Team