Node.js Tutorial to Print all Names of Files Present in a Directory Using fs.readdir() Method Full Project For Beginners

 

Welcome folks today in this blog post we will be printing all the files names which are present inside a certain directory in node.js.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started make a index.js file and here we are using the fs module

 

 

index.js

 

 

fs.readdir

const testFolder = './tests/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});

fs.readdirSync

const testFolder = './tests/';
const fs = require('fs');

fs.readdirSync(testFolder).forEach(file => {
  console.log(file);
});

Leave a Reply