node read dir code example

Example 1: nodejs readdir

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

Example 2: nodejs readfile

const fs = require('fs');

fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
  if (err) {
    console.error(err);
    return
  }
  console.log(data);
});

Example 3: read directory in node js

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

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

Example 4: fs readfile encoding

fs.readFile('/etc/passwd', 'utf8', callback);