Dir

external class Dir : AsyncIterable<Dirent> (source)

A class representing a directory stream.

Created by {@link opendir}, {@link opendirSync}, or fsPromises.opendir().

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

try {
const dir = await opendir('./');
for await (const dirent of dir)
console.log(dirent.name);
} catch (err) {
console.error(err);
}

When using the async iterator, the fs.Dir object will be automatically closed after the iterator exits.

Since

v12.12.0

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

The read-only path of this directory as was provided to {@link opendir},{@link opendirSync}, or fsPromises.opendir().

Functions

Link copied to clipboard

Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.

Link copied to clipboard
fun closeSync()

Synchronously close the directory's underlying resource handle. Subsequent reads will result in errors.

Link copied to clipboard
open operator fun get(key: Symbol.asyncIterator): () -> AsyncIterator<Dirent>
Link copied to clipboard
open inline operator fun iterator(): SuspendableIterator<Dirent>
Link copied to clipboard

Asynchronously read the next directory entry via readdir(3) as an fs.Dirent.

fun read(cb: (err: ErrnoException?, dirEnt: Dirent?) -> Unit)
Link copied to clipboard

Synchronously read the next directory entry as an fs.Dirent. See the POSIX readdir(3) documentation for more detail.