fs read folder code example

Example 1: node js get files in dir

const path = require('path');
const fs = require('fs');

fs.readdir(
  path.resolve(__dirname, 'MyFolder'),
  (err, files) => {
    if (err) throw err;
    
    for (let file of files) {
      console.log(file);
    }
  }
);

Example 2: how to get file name in directory node js

const path = require('path')

//...

//inside the `for` loop
const stat = fs.lstatSync(path.join(dir, file))

Example 3: fs.readdir example

const fs = require("fs")
fs.readdir("./myfolder", (err, data) => {
	if(err) throw err
  	console.log(data)
})