ItSolutionStuff.com

How to Get All Files from Folder in Node JS?

By Hardik Savani • September 14, 2021
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: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Generate QR Code in Node JS?

Read Now →

Node JS Send Email with Attachment Example

Read Now →

How to Send Email using Gmail Account in Node.js?

Read Now →

How to Get Current Date and Time in Node JS?

Read Now →

How to Get Client IP Address in Node JS?

Read Now →

How to Render HTML File in Node JS Express?

Read Now →

How to Create Separate Routes File in Node JS Express?

Read Now →

How to Get Query String Value in Node.js?

Read Now →

How to Generate 4,6,8,10 Digit Random number in Node JS?

Read Now →

Node JS Read and Write Json File Example

Read Now →

Node JS Http Request with Headers Example

Read Now →

Node JS Make Http Get Request with Parameters Example

Read Now →