Class Dir

A class representing a directory stream.

Created by opendir, opendirSync, or fsPromises.opendir().

import { opendir } from '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

Hierarchy

  • Dir

Implements

Constructors

Properties

Methods

Constructors

Properties

path: string

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

Since

v12.12.0

Methods

  • Asynchronously iterates over the directory via readdir(3) until all entries have been read.

    Returns AsyncIterableIterator<Dirent>

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

    A promise is returned that will be resolved after the resource has been closed.

    Returns Promise<void>

    Since

    v12.12.0

  • Parameters

    Returns void

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

    Returns void

    Since

    v12.12.0

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

    A promise is returned that will be resolved with an fs.Dirent, or null if there are no more directory entries to read.

    Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    Returns Promise<Dirent>

    containing {fs.Dirent|null}

    Since

    v12.12.0

  • Parameters

    • cb: ((err, dirEnt) => void)
        • (err, dirEnt): void
        • Parameters

          • err: ErrnoException
          • dirEnt: Dirent

          Returns void

    Returns void

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

    If there are no more directory entries to read, null will be returned.

    Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    Returns Dirent

    Since

    v12.12.0

Generated using TypeDoc