Skip to main content

readdir

function readdir
Jump to headingreaddir(
path: PathLike,
options?:
(ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; })
| BufferEncoding
| null
,
): Promise<string[]>

Reads the contents of a directory.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the filenames. If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

If options.withFileTypes is set to true, the returned array will contain fs.Dirent objects.

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

try {
  const files = await readdir(path);
  for (const file of files)
    console.log(file);
} catch (err) {
  console.error(err);
}

Parameters Jump to heading

optional
Jump to headingoptions:
(ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; })
| BufferEncoding
| null

Return Type Jump to heading

Promise<string[]>

Fulfills with an array of the names of the files in the directory excluding '.' and '..'.

Jump to headingreaddir(
path: PathLike,
options: { encoding: "buffer"; withFileTypes?: false | undefined; recursive?: boolean | undefined; } | "buffer",
): Promise<Buffer[]>

Asynchronous readdir(3) - read a directory.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

Jump to headingoptions: { encoding: "buffer"; withFileTypes?: false | undefined; recursive?: boolean | undefined; } | "buffer"

The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used.

Return Type Jump to heading

Promise<Buffer[]>
Jump to headingreaddir(
path: PathLike,
options?:
(ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; })
| BufferEncoding
| null
,
): Promise<string[] | Buffer[]>

Asynchronous readdir(3) - read a directory.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

optional
Jump to headingoptions:
(ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; })
| BufferEncoding
| null

The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used.

Return Type Jump to heading

Promise<string[] | Buffer[]>
Jump to headingreaddir(
path: PathLike,
options: ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; },
): Promise<Dirent[]>

Asynchronous readdir(3) - read a directory.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

Jump to headingoptions: ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; }

If called with withFileTypes: true the result data will be an array of Dirent.

Return Type Jump to heading

Promise<Dirent[]>
Back to top