Premium lesson

Working With Directories in Node.js

Node.js·2 min read·Jan 1, 2025

Reading the content of a directory

To list the content of a directory, you can use the fs.readdirSync() static method:

const files = fs.readdirSync(path, { withFileTypes?, recursive? });

Where:

  • files is an array of filenames.

  • path is the path to the target directory.

  • withFileTypes is an optional boolean used to return fs.Dirent objects instead of strings, including the type of each file.

  • recursive is an optional boolean used to recursively list the content of the target directory.

icon light bulb key

Unlock the Build CLI Apps in JavaScript & Node.js module

Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.

You get immediate access to:

  • 45 focused lessons across JavaScript, Node.js, npm, and Git
  • 17 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module