Nodejs Read Directories and Files via Await Async

May 6, 2022
// import { promises as fs } from 'fs';
// const { promises: fs } = require('fs');
const fs = require('fs');

const dir = ...
const files = await fs.promises.readdir(dir);

for (let file of files) {
  if (file.endsWith('.json')) {
    var data = JSON.parse(await fs.promises.readFile(`${dir}/${file}`, 'utf8'));
    console.log(file, data);
  }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.