How to Get All Files from Folder in Node JS?

By Hardik Savani September 14, 2021 Category : Node JS

In this example, i will show you node js get all files in directory. if you have question about how to get all files from folder in node js then i will give simple example with solution. This article will give you simple example of how to get all files in a directory node js. you can see node get all files in directory. Let's get started with nodejs get all files in directory with extension.

We will use fs node js package with readdir() and readdirSync() to getting all files from folder in node js app. let's see bellow two examples examples:

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure you have "uploads" folder in root directory with some files on that.

server.js

const folderPath = './uploads/';

const fs = require('fs');

/* Example Code 1 */

fs.readdir(folderPath, (err, files) => {

files.forEach(file => {

console.log(file);

});

});

/* Example Code 2 */

fs.readdirSync(folderPath).forEach(file => {

console.log(file);

});

now you can simply run by following command:

node server.js

Output:

i hope it can help you...

Tags :
Shares