Skip to content
VaelJS Blog

node:fs/promises

Node-compatible promise-based filesystem operations.

Available in vjs only (not available in vjs-lite). Enabled by default unless --no-node-compat is passed.

import ... from "node:fs/promises"
Since v0.0.0-alpha.0 vjs (V8)
Cargo feature: node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
await mkdir("/tmp/vael-promises-demo", { recursive: true });
await writeFile("/tmp/vael-promises-demo/note.txt", "async fs", "utf8");
console.log(await readFile("/tmp/vael-promises-demo/note.txt", "utf8"));
await rm("/tmp/vael-promises-demo", { recursive: true, force: true });
API Symbols
Functions (21)
readFilevjs onlyv0.0.0-alpha.0
Asynchronously read the entire contents of path as bytes.
writeFilevjs onlyv0.0.0-alpha.0
Asynchronously write data to path, replacing the file if it already exists.
appendFilevjs onlyv0.0.0-alpha.0
Asynchronously append data to path, creating the file if it does not exist.
statvjs onlyv0.0.0-alpha.0
Asynchronously return Stats for path, following symbolic links.
lstatvjs onlyv0.0.0-alpha.0
Asynchronously return Stats for path, without following symbolic links.
readdirvjs onlyv0.0.0-alpha.0
Asynchronously read directory entry names.
mkdirvjs onlyv0.0.0-alpha.0
Asynchronously create a directory at path.
rmvjs onlyv0.0.0-alpha.0
Asynchronously remove files and directories at path.
unlinkvjs onlyv0.0.0-alpha.0
Asynchronously remove a file or symbolic link at path.
renamevjs onlyv0.0.0-alpha.0
Asynchronously rename oldPath to newPath.
copyFilevjs onlyv0.0.0-alpha.0
Asynchronously copy src to dest.
realpathvjs onlyv0.0.0-alpha.0
Asynchronously compute the canonical pathname for path.
mkdtempvjs onlyv0.0.0-alpha.0
Asynchronously create a unique temporary directory starting with prefix.
openvjs onlyv0.0.0-alpha.2
Asynchronously open path and return a FileHandle.
accessvjs onlyv0.0.0-alpha.2
Asynchronously test user permissions for path.
symlinkvjs onlyv0.0.0-alpha.2
Asynchronously create a symbolic link at path pointing to target.
readlinkvjs onlyv0.0.0-alpha.2
Asynchronously read the target of the symbolic link at path as a string.
chmodvjs onlyv0.0.0-alpha.2
Asynchronously change the permissions of path.
cpvjs onlyv0.0.0-alpha.2
Asynchronously copy files or directories from src to dest.
truncatevjs onlyv0.0.0-alpha.2
Asynchronously truncate (or extend) the file at path to len bytes.
watchvjs onlyv0.0.0-alpha.2
Return an async iterator yielding filesystem change events for filename.
functionSince v0.0.0-alpha.0vjs only
export function readFile(path: PathInput, options?: { encoding?: null | undefined } | null): Promise<Uint8Array>;
export function readFile(path: PathInput, options: TextEncoding | { encoding: TextEncoding }): Promise<string>;

Asynchronously read the entire contents of path as bytes.

Asynchronously read the entire contents of path as an encoded string.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
options optional{ encoding?: null | undefined } | null | TextEncoding | { encoding: TextEncoding }Optional object with encoding: null | undefined (or null); Encoding name or { encoding } object.
Returns
  • Promise<Uint8Array> — Promise resolving to a Buffer (Uint8Array subclass) containing the file bytes.
  • Promise<string> — Promise resolving to the decoded string contents.
Exceptions
  • TypeError — on invalid path or options types; on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).
  • Error — with code "ENOENT", "EACCES", "EISDIR", "EINVAL", "ERR_FS_FILE_TOO_LARGE", or "EIO".
import { readFile } from "node:fs/promises";
const bytes = await readFile("/etc/hosts");
console.log(bytes.byteLength);
functionSince v0.0.0-alpha.0vjs only
export function writeFile(path: PathInput, data: string | Uint8Array | ArrayBufferView, options?: TextEncoding | { encoding?: TextEncoding | null }): Promise<void>;

Asynchronously write data to path, replacing the file if it already exists.

Parameters
NameTypeDescription
pathPathInputDestination filesystem path or file:// URL.
datastring | Uint8Array | ArrayBufferViewString or byte view to write.
options optionalTextEncoding | { encoding?: TextEncoding | null }Encoding for string data (default "utf8") or options object.
Returns
Promise<void> — Promise resolving when the write completes.
Exceptions
  • TypeError — on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).
  • Error — with code "ENOENT", "EACCES", "EISDIR", "EINVAL", or "EIO".
import { readFile, writeFile } from "node:fs/promises";
await writeFile("/tmp/vael-async.txt", "hello", "utf8");
console.log(await readFile("/tmp/vael-async.txt", "utf8"));
functionSince v0.0.0-alpha.0vjs only
export function appendFile(path: PathInput, data: string | Uint8Array | ArrayBufferView, options?: TextEncoding | { encoding?: TextEncoding | null }): Promise<void>;

Asynchronously append data to path, creating the file if it does not exist.

Parameters
NameTypeDescription
pathPathInputDestination filesystem path or file:// URL.
datastring | Uint8Array | ArrayBufferViewString or byte view to append.
options optionalTextEncoding | { encoding?: TextEncoding | null }Encoding for string data (default "utf8") or options object.
Returns
Promise<void> — Promise resolving when the append completes.
Exceptions
  • TypeError — on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).
  • Error — with code "ENOENT", "EACCES", "EISDIR", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function stat(path: PathInput): Promise<Stats>;

Asynchronously return Stats for path, following symbolic links.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
Returns
Promise<Stats> — Promise resolving to Stats for the resolved entry.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT", "EACCES", "EINVAL", or "EIO".
import { stat } from "node:fs/promises";
const info = await stat("/tmp");
console.log(info.isDirectory());
functionSince v0.0.0-alpha.0vjs only
export function lstat(path: PathInput): Promise<Stats>;

Asynchronously return Stats for path, without following symbolic links.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
Returns
Promise<Stats> — Promise resolving to Stats for the entry itself (including symlinks).
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function readdir(path: PathInput, options?: { withFileTypes?: false }): Promise<string[]>;
export function readdir(path: PathInput, options: { withFileTypes: true }): Promise<Dirent[]>;

Asynchronously read directory entry names.

Asynchronously read directory entries as Dirent objects.

Parameters
NameTypeDescription
pathPathInputDirectory path or file:// URL.
options optional{ withFileTypes?: false } | { withFileTypes: true }Optional { withFileTypes?: false }; { withFileTypes: true }.
Returns
  • Promise<string[]> — Promise resolving to an array of entry names inside path.
  • Promise<Dirent[]> — Promise resolving to an array of Dirent objects inside path.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT", "ENOTDIR", "EACCES", "EINVAL", or "EIO".
import { readdir } from "node:fs/promises";
const names = await readdir("/tmp");
console.log(names.length);
functionSince v0.0.0-alpha.0vjs only
export function mkdir(path: PathInput, options?: { recursive?: boolean }): Promise<void>;

Asynchronously create a directory at path.

Parameters
NameTypeDescription
pathPathInputDirectory path or file:// URL.
options optional{ recursive?: boolean }Set recursive: true to create missing parent directories and succeed if already present.
Returns
Promise<void> — Promise resolving when the directory exists.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "EEXIST", "ENOENT", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function rm(path: PathInput, options?: { recursive?: boolean; force?: boolean }): Promise<void>;

Asynchronously remove files and directories at path.

Parameters
NameTypeDescription
pathPathInputTarget path or file:// URL.
options optional{ recursive?: boolean; force?: boolean }Set recursive: true to remove directory trees and force: true to ignore missing paths (ENOENT).
Returns
Promise<void> — Promise resolving when removal completes.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT" (unless force: true), "EISDIR", "ENOTEMPTY", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function unlink(path: PathInput): Promise<void>;

Asynchronously remove a file or symbolic link at path.

Parameters
NameTypeDescription
pathPathInputFile or symlink path or file:// URL.
Returns
Promise<void> — Promise resolving when the entry is removed.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT", "EISDIR", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function rename(oldPath: PathInput, newPath: PathInput): Promise<void>;

Asynchronously rename oldPath to newPath.

Parameters
NameTypeDescription
oldPathPathInputExisting source path or file:// URL.
newPathPathInputDestination path or file:// URL.
Returns
Promise<void> — Promise resolving when the rename completes.
Exceptions
  • TypeError — when oldPath or newPath has an invalid type.
  • Error — with code "ENOENT", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function copyFile(src: PathInput, dest: PathInput, mode?: number): Promise<void>;

Asynchronously copy src to dest.

Parameters
NameTypeDescription
srcPathInputSource file path or file:// URL.
destPathInputDestination file path or file:// URL.
mode optionalnumberOptional bitmask (pass constants.COPYFILE_EXCL to fail with EEXIST if dest exists).
Returns
Promise<void> — Promise resolving when the copy completes.
Exceptions
  • TypeError — when src or dest has an invalid type.
  • Error — with code "ENOENT", "EEXIST", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function realpath(path: PathInput): Promise<string>;

Asynchronously compute the canonical pathname for path.

Parameters
NameTypeDescription
pathPathInputPath or file:// URL to canonicalize.
Returns
Promise<string> — Promise resolving to the canonicalized path string.
Exceptions
  • TypeError — when path has an invalid type.
  • Error — with code "ENOENT", "EACCES", "EINVAL", or "EIO".
functionSince v0.0.0-alpha.0vjs only
export function mkdtemp(prefix: string): Promise<string>;

Asynchronously create a unique temporary directory starting with prefix.

Appends 6 random alphanumeric characters to prefix and creates the directory (mode 0o700 on Unix). The parent directory must already exist.

Parameters
NameTypeDescription
prefixstringPath prefix for the new temporary directory.
Returns
Promise<string> — Promise resolving to the path of the created temporary directory.
Exceptions
  • TypeError — when prefix is not a string.
  • Error — with code "EINVAL" (empty/NUL prefix), "ENOENT", "EACCES", or "EIO".
import { mkdtemp, rm } from "node:fs/promises";
const dir = await mkdtemp("/tmp/vael-");
console.log(dir);
await rm(dir, { recursive: true, force: true });
functionSince v0.0.0-alpha.2vjs only
export function open(
  path: PathInput,
  flags?: string | number,
  mode?: string | number,
): Promise<FileHandle>;

Asynchronously open path and return a FileHandle.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
flags optionalstring | numberOpen flags string or numeric bitmask (default "r").
mode optionalstring | numberPOSIX file permissions when creating a file (default 0o666).
Returns
Promise<FileHandle> — Promise resolving to an open FileHandle.
functionSince v0.0.0-alpha.2vjs only
export function access(path: PathInput, mode?: number): Promise<void>;

Asynchronously test user permissions for path.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
mode optionalnumberOptional accessibility bitmask (F_OK, R_OK, W_OK, X_OK, default F_OK).
Returns
Promise<void> — Promise resolving if accessible, or rejecting on failure.
functionSince v0.0.0-alpha.2vjs only
export function symlink(
  target: PathInput,
  path: PathInput,
  type?: "dir" | "file" | "junction",
): Promise<void>;

Asynchronously create a symbolic link at path pointing to target.

Parameters
NameTypeDescription
targetPathInputLink target path.
pathPathInputSymbolic link path to create.
type optional"dir" | "file" | "junction"Optional Windows symlink hint ("dir", "file", or "junction").
Returns
Promise<void> — Promise resolving when the symlink is created.
functionSince v0.0.0-alpha.2vjs only
export function readlink(
  path: PathInput,
  options?: TextEncoding | { encoding?: TextEncoding } | null,
): Promise<string>;
export function readlink(
  path: PathInput,
  options: "buffer" | { encoding: "buffer" },
): Promise<Uint8Array>;

Asynchronously read the target of the symbolic link at path as a string.

Asynchronously read the target of the symbolic link at path as raw bytes.

Parameters
NameTypeDescription
pathPathInputSymbolic link path or file:// URL.
options optionalTextEncoding | { encoding?: TextEncoding } | null | "buffer" | { encoding: "buffer" }Optional encoding ("utf8" default); { encoding: "buffer" }.
Returns
  • Promise<string> — Promise resolving to the symlink target string.
  • Promise<Uint8Array> — Promise resolving to the symlink target as Uint8Array.
functionSince v0.0.0-alpha.2vjs only
export function chmod(path: PathInput, mode: string | number): Promise<void>;

Asynchronously change the permissions of path.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
modestring | numberNumeric or octal string file mode.
Returns
Promise<void> — Promise resolving when permissions are updated.
functionSince v0.0.0-alpha.2vjs only
export function cp(src: PathInput, dest: PathInput, options?: CpOptions): Promise<void>;

Asynchronously copy files or directories from src to dest.

Parameters
NameTypeDescription
srcPathInputSource file or directory path.
destPathInputDestination path.
options optionalCpOptionsOptional copy behavior (recursive, force, filter, etc.).
Returns
Promise<void> — Promise resolving when the copy completes.
functionSince v0.0.0-alpha.2vjs only
export function truncate(path: PathInput, len?: number): Promise<void>;

Asynchronously truncate (or extend) the file at path to len bytes.

Parameters
NameTypeDescription
pathPathInputFilesystem path or file:// URL.
len optionalnumberTarget byte length (default 0).
Returns
Promise<void> — Promise resolving when truncation completes.
functionSince v0.0.0-alpha.2vjs only
export function watch(
  filename: PathInput,
  options?: WatchOptions | TextEncoding,
): AsyncIterableIterator<FileChangeInfo>;

Return an async iterator yielding filesystem change events for filename.

Parameters
NameTypeDescription
filenamePathInputFile or directory path to watch.
options optionalWatchOptions | TextEncodingOptional watch settings or encoding string.
Returns
AsyncIterableIterator<FileChangeInfo> — Async iterator of FileChangeInfo events.
interfaceSince v0.0.0-alpha.0vjs only
export interface Stats {
  readonly size: number;
  readonly mode: number;
  readonly atimeMs: number;
  readonly atime: Date;
  readonly mtimeMs: number;
  readonly mtime: Date;
  readonly ctimeMs: number;
  readonly ctime: Date;
  readonly birthtimeMs: number;
  readonly birthtime: Date;
  isFile(): boolean;
  isDirectory(): boolean;
  isSymbolicLink(): boolean;
  isFIFO(): boolean;
  isSocket(): boolean;
  isBlockDevice(): boolean;
  isCharacterDevice(): boolean;
}

File metadata returned by stat and lstat.

Properties
PropertyTypeDescriptionSince
size readonlynumberSize of the file in bytes.v0.0.0-alpha.0
mode readonlynumberPOSIX file type and permissions bitmask.v0.0.0-alpha.2
atimeMs readonlynumberLast access timestamp in milliseconds since the Unix epoch.v0.0.0-alpha.2
atime readonlyDateLast access timestamp as a Date instance.v0.0.0-alpha.2
mtimeMs readonlynumberLast modification timestamp in milliseconds since the Unix epoch.v0.0.0-alpha.0
mtime readonlyDateLast modification timestamp as a Date instance.v0.0.0-alpha.0
ctimeMs readonlynumberLast status change timestamp in milliseconds since the Unix epoch.v0.0.0-alpha.2
ctime readonlyDateLast status change timestamp as a Date instance.v0.0.0-alpha.2
birthtimeMs readonlynumberFile creation (birthtime) timestamp in milliseconds since the Unix epoch.v0.0.0-alpha.2
birthtime readonlyDateFile creation (birthtime) timestamp as a Date instance.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.0
isFile(): boolean;

Return true if the entry is a regular file.

Returns
boolean — true for a regular file, otherwise false.
methodSince v0.0.0-alpha.0
isDirectory(): boolean;

Return true if the entry is a directory.

Returns
boolean — true for a directory, otherwise false.
methodSince v0.0.0-alpha.0
isSymbolicLink(): boolean;

Return true if the entry is a symbolic link.

Returns
boolean — true for a symbolic link, otherwise false.
methodSince v0.0.0-alpha.2
isFIFO(): boolean;

Return true if the entry is a FIFO (named pipe).

Returns
boolean — true for a FIFO, otherwise false.
methodSince v0.0.0-alpha.2
isSocket(): boolean;

Return true if the entry is a socket.

Returns
boolean — true for a socket, otherwise false.
methodSince v0.0.0-alpha.2
isBlockDevice(): boolean;

Return true if the entry is a block device.

Returns
boolean — true for a block device, otherwise false.
methodSince v0.0.0-alpha.2
isCharacterDevice(): boolean;

Return true if the entry is a character device.

Returns
boolean — true for a character device, otherwise false.
interfaceSince v0.0.0-alpha.0vjs only
export interface Dirent {
  readonly name: string;
  readonly parentPath: string;
  isFile(): boolean;
  isDirectory(): boolean;
  isSymbolicLink(): boolean;
}

Directory entry returned by readdir with withFileTypes: true.

Properties
PropertyTypeDescriptionSince
name readonlystringEntry filename relative to the directory passed to readdir.v0.0.0-alpha.0
parentPath readonlystringPath to the parent directory containing this entry.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.0
isFile(): boolean;

Return true if the entry is a regular file.

Returns
boolean — true for a regular file, otherwise false.
methodSince v0.0.0-alpha.0
isDirectory(): boolean;

Return true if the entry is a directory.

Returns
boolean — true for a directory, otherwise false.
methodSince v0.0.0-alpha.0
isSymbolicLink(): boolean;

Return true if the entry is a symbolic link.

Returns
boolean — true for a symbolic link, otherwise false.
interfaceSince v0.0.0-alpha.0vjs only
export interface FsConstants {
  readonly F_OK: number;
  readonly R_OK: number;
  readonly W_OK: number;
  readonly X_OK: number;
  readonly COPYFILE_EXCL: number;
  readonly COPYFILE_FICLONE: number;
  readonly COPYFILE_FICLONE_FORCE: number;
}

Filesystem access and copy mode constants.

Properties
PropertyTypeDescriptionSince
F_OK readonlynumberFlag testing existence of the path (0).v0.0.0-alpha.0
R_OK readonlynumberFlag testing read permission (4).v0.0.0-alpha.0
W_OK readonlynumberFlag testing write permission (2).v0.0.0-alpha.0
X_OK readonlynumberFlag testing execute permission (1).v0.0.0-alpha.0
COPYFILE_EXCL readonlynumberCopy flag failing if the destination file already exists (1).v0.0.0-alpha.0
COPYFILE_FICLONE readonlynumberCopy flag requesting copy-on-write reflink if supported (2).v0.0.0-alpha.0
COPYFILE_FICLONE_FORCE readonlynumberCopy flag requiring copy-on-write reflink (4).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.2vjs only
export interface AbortSignalLike {
  readonly aborted: boolean;
  readonly reason?: unknown;
  addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
  removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}

Minimal abort-signal contract accepted by watch().

Properties
PropertyTypeDescriptionSince
aborted readonlybooleanWhether the signal has already been aborted.v0.0.0-alpha.2
reason readonly optionalunknownOptional abort reason value.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;

Register an "abort" listener.

Parameters
NameTypeDescription
typestringEvent type ("abort").
listener(...args: any[]) => voidCallback invoked on abort.
options optional{ once?: boolean }Optional listener options.
Returns
void
methodSince v0.0.0-alpha.2
removeEventListener?(type: string, listener: (...args: any[]) => void): void;

Unregister an "abort" listener.

Parameters
NameTypeDescription
typestringEvent type ("abort").
listener(...args: any[]) => voidCallback to remove.
Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface CpOptions {
  dereference?: boolean;
  errorOnExist?: boolean;
  filter?: (source: string, destination: string) => boolean | Promise<boolean>;
  force?: boolean;
  recursive?: boolean;
  preserveTimestamps?: boolean;
  verbatimSymlinks?: boolean;
}

Options accepted by cp().

Properties
PropertyTypeDescriptionSince
dereference optionalbooleanDereference symbolic links during copy.v0.0.0-alpha.2
errorOnExist optionalbooleanThrow ERR_FS_CP_EEXIST when force is false and target exists.v0.0.0-alpha.2
filter optional(source: string, destination: string) => boolean | Promise<boolean>Filter predicate (src, dest) => boolean | Promise<boolean>.v0.0.0-alpha.2
force optionalbooleanOverwrite existing files (default true).v0.0.0-alpha.2
recursive optionalbooleanCopy directories recursively (default false).v0.0.0-alpha.2
preserveTimestamps optionalbooleanPreserve file timestamps when true.v0.0.0-alpha.2
verbatimSymlinks optionalbooleanPreserve symlink behavior without following links.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface WatchOptions {
  persistent?: boolean;
  recursive?: boolean;
  encoding?: TextEncoding | "buffer";
  signal?: AbortSignalLike;
}

Options accepted by watch().

Properties
PropertyTypeDescriptionSince
persistent optionalbooleanKeep the event loop active while watching (default true).v0.0.0-alpha.2
recursive optionalbooleanWatch subdirectories recursively when true.v0.0.0-alpha.2
encoding optionalTextEncoding | "buffer"Character encoding for emitted filename strings.v0.0.0-alpha.2
signal optionalAbortSignalLikeOptional abort signal stopping the watcher.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface FileChangeInfo {
  eventType: "rename" | "change";
  filename: string | null;
}

Change event object yielded by fs/promises.watch().

Properties
PropertyTypeDescriptionSince
eventType "rename" | "change"Filesystem change event kind ("rename" or "change").v0.0.0-alpha.2
filename string | nullChanged filename relative to the watched path, or null.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface FileReadResult<T extends ArrayBufferView = ArrayBufferView> {
  bytesRead: number;
  buffer: T;
}

Result returned by FileHandle.read().

Properties
PropertyTypeDescriptionSince
bytesRead numberNumber of bytes read into buffer.v0.0.0-alpha.2
buffer TTarget buffer populated by the read.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface FileWriteResult<T = ArrayBufferView | string> {
  bytesWritten: number;
  buffer: T;
}

Result returned by FileHandle.write().

Properties
PropertyTypeDescriptionSince
bytesWritten numberNumber of bytes written.v0.0.0-alpha.2
buffer TSource buffer or string passed to write().v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface FileHandle {
  readonly fd: number;
  read<T extends ArrayBufferView>(
      buffer: T,
      offset?: number | null,
      length?: number | null,
      position?: number | null,
    ): Promise<FileReadResult<T>>;
  write<T extends ArrayBufferView>(
      buffer: T,
      offset?: number | null,
      length?: number | null,
      position?: number | null,
    ): Promise<FileWriteResult<T>>;
  write(data: string, position?: number | null, encoding?: TextEncoding): Promise<FileWriteResult<string>>;
  readFile(options?: { encoding?: null | undefined } | null): Promise<Uint8Array>;
  readFile(options: TextEncoding | { encoding: TextEncoding }): Promise<string>;
  writeFile(
      data: string | Uint8Array | ArrayBufferView,
      options?: TextEncoding | { encoding?: TextEncoding | null },
    ): Promise<void>;
  appendFile(
      data: string | Uint8Array | ArrayBufferView,
      options?: TextEncoding | { encoding?: TextEncoding | null },
    ): Promise<void>;
  stat(): Promise<Stats>;
  sync(): Promise<void>;
  datasync(): Promise<void>;
  truncate(len?: number): Promise<void>;
  chmod(mode: string | number): Promise<void>;
  close(): Promise<void>;
  [Symbol.asyncDispose](): Promise<void>;
}

Promise-based open file descriptor wrapper returned by open().

Properties
PropertyTypeDescriptionSince
fd readonlynumberUnderlying numeric file descriptor.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
read<T extends ArrayBufferView>(
  buffer: T,
  offset?: number | null,
  length?: number | null,
  position?: number | null,
): Promise<FileReadResult<T>>;

Read bytes from the file handle into buffer.

Parameters
NameTypeDescription
bufferT—
offset optionalnumber | null—
length optionalnumber | null—
position optionalnumber | null—
methodSince v0.0.0-alpha.2
write<T extends ArrayBufferView>(
  buffer: T,
  offset?: number | null,
  length?: number | null,
  position?: number | null,
): Promise<FileWriteResult<T>>;
write(data: string, position?: number | null, encoding?: TextEncoding): Promise<FileWriteResult<string>>;

Write bytes from buffer to the file handle.

Overload 1 — write(buffer, offset?, length?, position?)
NameTypeDescription
bufferT—
offset optionalnumber | null—
length optionalnumber | null—
position optionalnumber | null—
Overload 2 — write(data, position?, encoding?)

Write string data to the file handle.

NameTypeDescription
datastring—
position optionalnumber | null—
encoding optionalTextEncoding—
methodSince v0.0.0-alpha.2
readFile(options?: { encoding?: null | undefined } | null): Promise<Uint8Array>;
readFile(options: TextEncoding | { encoding: TextEncoding }): Promise<string>;

Read remaining contents of the handle as bytes.

Read remaining contents of the handle as a decoded string.

Parameters
NameTypeDescription
options optional{ encoding?: null | undefined } | null | TextEncoding | { encoding: TextEncoding }—
Returns
methodSince v0.0.0-alpha.2
writeFile(
  data: string | Uint8Array | ArrayBufferView,
  options?: TextEncoding | { encoding?: TextEncoding | null },
): Promise<void>;

Write data to the file handle.

Parameters
NameTypeDescription
datastring | Uint8Array | ArrayBufferView—
options optionalTextEncoding | { encoding?: TextEncoding | null }—
Returns
Promise<void>
methodSince v0.0.0-alpha.2
appendFile(
  data: string | Uint8Array | ArrayBufferView,
  options?: TextEncoding | { encoding?: TextEncoding | null },
): Promise<void>;

Append data to the file handle.

Parameters
NameTypeDescription
datastring | Uint8Array | ArrayBufferView—
options optionalTextEncoding | { encoding?: TextEncoding | null }—
Returns
Promise<void>
methodSince v0.0.0-alpha.2
stat(): Promise<Stats>;

Return Stats for the open file handle.

Returns
methodSince v0.0.0-alpha.2
sync(): Promise<void>;

Flush modified file data and metadata to disk.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
datasync(): Promise<void>;

Flush modified file data (without metadata) to disk.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
truncate(len?: number): Promise<void>;

Truncate or extend the file to len bytes (default 0).

Parameters
NameTypeDescription
len optionalnumber—
Returns
Promise<void>
methodSince v0.0.0-alpha.2
chmod(mode: string | number): Promise<void>;

Change file permissions on the open handle.

Parameters
NameTypeDescription
modestring | number—
Returns
Promise<void>
methodSince v0.0.0-alpha.2
close(): Promise<void>;

Close the file handle.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
[Symbol.asyncDispose](): Promise<void>;

Close the file handle automatically on await using.

Returns
Promise<void>
typeSince v0.0.0-alpha.0vjs only
export type TextEncoding = "utf8" | "utf-8" | "hex" | "base64" | "base64url" | "latin1" | "ascii";

Supported text encodings for filesystem string operations.

typeSince v0.0.0-alpha.0vjs only
export type PathInput = string | { readonly href: string; readonly protocol: string };

Filesystem path string or file:// URL-like object.

constSince v0.0.0-alpha.0vjs only
export const constants: FsConstants;

Filesystem access and copy mode constants.

defaultSince v0.0.0-alpha.0vjs only
const _default: {
  /** Asynchronously read file contents. @since 0.0.0-alpha.0 */
  readFile: typeof readFile;
  /** Asynchronously write file contents. @since 0.0.0-alpha.0 */
  writeFile: typeof writeFile;
  /** Asynchronously append file contents. @since 0.0.0-alpha.0 */
  appendFile: typeof appendFile;
  /** Asynchronously stat a path, following symlinks. @since 0.0.0-alpha.0 */
  stat: typeof stat;
  /** Asynchronously stat a path without following symlinks. @since 0.0.0-alpha.0 */
  lstat: typeof lstat;
  /** Asynchronously read directory entries. @since 0.0.0-alpha.0 */
  readdir: typeof readdir;
  /** Asynchronously create a directory. @since 0.0.0-alpha.0 */
  mkdir: typeof mkdir;
  /** Asynchronously remove files or directories. @since 0.0.0-alpha.0 */
  rm: typeof rm;
  /** Asynchronously remove a file or symlink. @since 0.0.0-alpha.0 */
  unlink: typeof unlink;
  /** Asynchronously rename a file or directory. @since 0.0.0-alpha.0 */
  rename: typeof rename;
  /** Asynchronously copy a file. @since 0.0.0-alpha.0 */
  copyFile: typeof copyFile;
  /** Asynchronously canonicalize a path. @since 0.0.0-alpha.0 */
  realpath: typeof realpath;
  /** Asynchronously create a unique temporary directory. @since 0.0.0-alpha.0 */
  mkdtemp: typeof mkdtemp;
  /** Asynchronously open a file handle. @since 0.0.0-alpha.2 */
  open: typeof open;
  /** Asynchronously check file accessibility. @since 0.0.0-alpha.2 */
  access: typeof access;
  /** Asynchronously create a symbolic link. @since 0.0.0-alpha.2 */
  symlink: typeof symlink;
  /** Asynchronously read a symbolic link target. @since 0.0.0-alpha.2 */
  readlink: typeof readlink;
  /** Asynchronously change file permissions. @since 0.0.0-alpha.2 */
  chmod: typeof chmod;
  /** Asynchronously copy files or directories. @since 0.0.0-alpha.2 */
  cp: typeof cp;
  /** Asynchronously truncate a file. @since 0.0.0-alpha.2 */
  truncate: typeof truncate;
  /** Asynchronously watch for file changes. @since 0.0.0-alpha.2 */
  watch: typeof watch;
  /** Filesystem access and copy mode constants. @since 0.0.0-alpha.0 */
  readonly constants: FsConstants;
};
export default _default;

Default export object providing all node:fs/promises functions and constants.