Dir

external class Dir : AsyncIterable.Mixin<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
external val path: String

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

Functions

Link copied to clipboard
open external override fun [@@asyncIterator](): AsyncIterator<Dirent<*>>
Link copied to clipboard
suspend external fun close(): Void
external fun close(cb: NoParamCallback)
Link copied to clipboard
external fun closeAsync(): Promise<Void>

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

Link copied to clipboard
external fun closeSync()

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

Link copied to clipboard
suspend external fun read(): Dirent<*>?
external fun read(cb: (err: ErrnoException?, dirEnt: Dirent<*>?) -> Unit)
Link copied to clipboard
external fun readAsync(): Promise<Dirent<*>?>

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

Link copied to clipboard
external fun readSync(): Dirent<*>?

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