Filesystem
fs/promises.opendir()
Opens a directory for iterative reading. The async iterator closes the directory when iteration finishes. Tested on Node.js 22.16.0. Example output (one line per entry): ["book.txt"].
Formal Syntax & Structure
nodejs syntaxnodejs
opendir(path[, options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| path | string | URL | Directory path. |
| options | object | Directory read options. |
Return Value
Promise<Dir>
Directory handle.
Working Example
Example Snippetnodejs
import * as fs from 'node:fs/promises';import { constants } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = await fs.mkdtemp(join(tmpdir(), 'bytemind-'));try { const p = join(dir, 'book.txt'); await fs.writeFile(p, 'catalog'); const d = await fs.opendir(dir); const names = []; for await (const e of d) names.push(e.name); console.log(names.sort().join(','));}finally { await fs.rm(dir, { recursive: true, force: true });}