How to Get All Files from Folder in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Generate QR Code in Node JS?
- Node JS Send Email with Attachment Example
- How to Send Email using Gmail Account in Node.js?
- How to Get Current Date and Time in Node JS?
- How to Get Client IP Address in Node JS?
- How to Render HTML File in Node JS Express?
- How to Create Separate Routes File in Node JS Express?
- How to Get Query String Value in Node.js?
- How to Generate 4,6,8,10 Digit Random number in Node JS?
- Node JS Read and Write Json File Example
- Node JS Http Request with Headers Example
- Node JS Make Http Get Request with Parameters Example