Count the number of files in a directory using JavaScript/nodejs?

Using fs, I found retrieving the directory file count to be straightforward.

const fs = require('fs');
const dir="./directory";

fs.readdir(dir, (err, files) => {
  console.log(files.length);
});

For the TS enthusiasts:

fs.readdir(dir, (err: NodeJS.ErrnoException | null, files: string[]) => {
  console.log(files.length);
});

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)