fs.readdir()

Reads the contents of a directory.

Since Node 10 (fs/promises); recursive Node 18.17 Spec ↗

Syntax

await readdir(path[, options])

Parameters

NameTypeRequiredDescription
path string | Buffer | URL Yes The directory to read.
options object | string No Encoding string, or an object with `encoding`, `withFileTypes` (return Dirent objects), and `recursive` (walk subdirectories).

Returns

Promise<string[] | Dirent[]> — Resolves with the directory entry names.

Examples

import { readdir } from 'node:fs/promises';

const names = await readdir('./src');
console.log(names);
Output
[ 'index.js', 'utils.js', 'lib' ]
import { readdir } from 'node:fs/promises';

const entries = await readdir('.', { withFileTypes: true });
const dirs = entries.filter((e) => e.isDirectory()).map((e) => e.name);
console.log(dirs);
Output
[ 'src', 'test', 'node_modules' ]

Notes

Entries are returned in no guaranteed order; sort them yourself if order matters. `withFileTypes: true` avoids an extra `stat` per entry. `recursive: true` (Node 18.17+) returns relative paths.

See also