Reads the contents of a directory.
The optional options
argument can be a string specifying an encoding, or an object with an encoding
property specifying the character encoding to use for the filenames. If the encoding
is set to 'buffer'
, the filenames returned will be passed as Buffer
objects.
If options.withFileTypes
is set to true
, the returned array will contain fs.Dirent
objects.
import { readdir } from 'node:fs/promises';
try {
const files = await readdir(path);
for (const file of files)
console.log(file);
} catch (err) {
console.error(err);
}