class
fs.Dir
class Dir
A class representing a directory stream.
Created by opendir, 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.
- readonly path: string
The read-only path of this directory as was provided to opendir,opendirSync, or
fsPromises.opendir(). Calls
dir.close()if the directory handle is open, and returns a promise that fulfills when disposal is complete.Asynchronously iterates over the directory via
readdir(3)until all entries have been read.Calls
dir.closeSync()if the directory handle is open, and returnsundefined.Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.
A promise is returned that will be fulfilled after the resource has been closed.
): void;Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.
A promise is returned that will be fulfilled after the resource has been closed.
Synchronously close the directory's underlying resource handle. Subsequent reads will result in errors.
Asynchronously read the next directory entry via
readdir(3)as anfs.Dirent.A promise is returned that will be fulfilled with an
fs.Dirent, ornullif 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.
@returnscontaining {fs.Dirent|null}
read(): void;Asynchronously read the next directory entry via
readdir(3)as anfs.Dirent.A promise is returned that will be fulfilled with an
fs.Dirent, ornullif 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.
@returnscontaining {fs.Dirent|null}
Synchronously read the next directory entry as an
fs.Dirent. See the POSIXreaddir(3)documentation for more detail.If there are no more directory entries to read,
nullwill 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.