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:
-
filesis an array of filenames. -
pathis the path to the target directory. -
withFileTypesis an optional boolean used to returnfs.Direntobjects instead of strings, including the type of each file. -
recursiveis an optional boolean used to recursively list the content of the target directory.
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