From the course: Node.js Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Listing directory files

Listing directory files - Node.js Tutorial

From the course: Node.js Essential Training

Listing directory files

- [Instructor] Node.js also ships with a module that allows us to interact with the file system. The Fs module can be used to list files and directories, create files and directories, stream files, change file permissions, and really just about anything you'd need to work with files and directories. To get started, we want to go over to this empty file called list.js and we want to create an import. So we'll say require fs. From here we're going to create a variable called files. That's going to help us read files into a variable. So we'll say fs.readdirSync and we want to read from the directory that we're currently in. Now, if we console log these files and we save and run node list, this is going to give us an array of all of the files that are listed here. Notice that when I read the directory, I used a function called read dir sync. This means that I read the contents of this directory synchronously with a blocking…

Contents