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"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”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 });readFilevjs onlyv0.0.0-alpha.0path as bytes.writeFilevjs onlyv0.0.0-alpha.0data to path, replacing the file if it already exists.appendFilevjs onlyv0.0.0-alpha.0data to path, creating the file if it does not exist.statvjs onlyv0.0.0-alpha.0Stats for path, following symbolic links.lstatvjs onlyv0.0.0-alpha.0Stats for path, without following symbolic links.readdirvjs onlyv0.0.0-alpha.0mkdirvjs onlyv0.0.0-alpha.0path.rmvjs onlyv0.0.0-alpha.0path.unlinkvjs onlyv0.0.0-alpha.0path.renamevjs onlyv0.0.0-alpha.0oldPath to newPath.copyFilevjs onlyv0.0.0-alpha.0src to dest.realpathvjs onlyv0.0.0-alpha.0path.mkdtempvjs onlyv0.0.0-alpha.0prefix.openvjs onlyv0.0.0-alpha.2path and return a FileHandle.accessvjs onlyv0.0.0-alpha.2path.symlinkvjs onlyv0.0.0-alpha.2path pointing to target.readlinkvjs onlyv0.0.0-alpha.2path as a string.chmodvjs onlyv0.0.0-alpha.2path.cpvjs onlyv0.0.0-alpha.2src to dest.truncatevjs onlyv0.0.0-alpha.2path to len bytes.watchvjs onlyv0.0.0-alpha.2filename.Statsvjs onlyv0.0.0-alpha.0stat and lstat.Direntvjs onlyv0.0.0-alpha.0readdir with withFileTypes: true.FsConstantsvjs onlyv0.0.0-alpha.0AbortSignalLikevjs onlyv0.0.0-alpha.2watch().CpOptionsvjs onlyv0.0.0-alpha.2cp().WatchOptionsvjs onlyv0.0.0-alpha.2watch().FileChangeInfovjs onlyv0.0.0-alpha.2fs/promises.watch().FileReadResultvjs onlyv0.0.0-alpha.2FileHandle.read().FileWriteResultvjs onlyv0.0.0-alpha.2FileHandle.write().FileHandlevjs onlyv0.0.0-alpha.2open().Functions
Section titled “Functions”readFile
Section titled “readFile”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.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem 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. |
Promise<Uint8Array>— Promise resolving to aBuffer(Uint8Arraysubclass) containing the file bytes.Promise<string>— Promise resolving to the decoded string contents.
TypeError— on invalidpathoroptionstypes; on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).Error— withcode"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);writeFile
Section titled “writeFile”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.
| Name | Type | Description |
|---|---|---|
path | PathInput | Destination filesystem path or file:// URL. |
data | string | Uint8Array | ArrayBufferView | String or byte view to write. |
options optional | TextEncoding | { encoding?: TextEncoding | null } | Encoding for string data (default "utf8") or options object. |
Promise<void> — Promise resolving when the write completes.TypeError— on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).Error— withcode"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"));appendFile
Section titled “appendFile”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.
| Name | Type | Description |
|---|---|---|
path | PathInput | Destination filesystem path or file:// URL. |
data | string | Uint8Array | ArrayBufferView | String or byte view to append. |
options optional | TextEncoding | { encoding?: TextEncoding | null } | Encoding for string data (default "utf8") or options object. |
Promise<void> — Promise resolving when the append completes.TypeError— on invalid argument types or unknown encoding (ERR_UNKNOWN_ENCODING).Error— withcode"ENOENT","EACCES","EISDIR","EINVAL", or"EIO".
export function stat(path: PathInput): Promise<Stats>;
Asynchronously return Stats for path, following symbolic links.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
TypeError— whenpathhas an invalid type.Error— withcode"ENOENT","EACCES","EINVAL", or"EIO".
import { stat } from "node:fs/promises";
const info = await stat("/tmp");console.log(info.isDirectory());export function lstat(path: PathInput): Promise<Stats>;
Asynchronously return Stats for path, without following symbolic links.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
TypeError— whenpathhas an invalid type.Error— withcode"ENOENT","EACCES","EINVAL", or"EIO".
readdir
Section titled “readdir”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.
| Name | Type | Description |
|---|---|---|
path | PathInput | Directory path or file:// URL. |
options optional | { withFileTypes?: false } | { withFileTypes: true } | Optional { withFileTypes?: false }; { withFileTypes: true }. |
TypeError— whenpathhas an invalid type.Error— withcode"ENOENT","ENOTDIR","EACCES","EINVAL", or"EIO".
import { readdir } from "node:fs/promises";
const names = await readdir("/tmp");console.log(names.length);export function mkdir(path: PathInput, options?: { recursive?: boolean }): Promise<void>;
Asynchronously create a directory at path.
| Name | Type | Description |
|---|---|---|
path | PathInput | Directory path or file:// URL. |
options optional | { recursive?: boolean } | Set recursive: true to create missing parent directories and succeed if already present. |
Promise<void> — Promise resolving when the directory exists.TypeError— whenpathhas an invalid type.Error— withcode"EEXIST","ENOENT","EACCES","EINVAL", or"EIO".
export function rm(path: PathInput, options?: { recursive?: boolean; force?: boolean }): Promise<void>;
Asynchronously remove files and directories at path.
| Name | Type | Description |
|---|---|---|
path | PathInput | Target 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). |
Promise<void> — Promise resolving when removal completes.TypeError— whenpathhas an invalid type.Error— withcode"ENOENT"(unlessforce: true),"EISDIR","ENOTEMPTY","EACCES","EINVAL", or"EIO".
unlink
Section titled “unlink”export function unlink(path: PathInput): Promise<void>;
Asynchronously remove a file or symbolic link at path.
| Name | Type | Description |
|---|---|---|
path | PathInput | File or symlink path or file:// URL. |
Promise<void> — Promise resolving when the entry is removed.TypeError— whenpathhas an invalid type.Error— withcode"ENOENT","EISDIR","EACCES","EINVAL", or"EIO".
rename
Section titled “rename”export function rename(oldPath: PathInput, newPath: PathInput): Promise<void>;
Asynchronously rename oldPath to newPath.
| Name | Type | Description |
|---|---|---|
oldPath | PathInput | Existing source path or file:// URL. |
newPath | PathInput | Destination path or file:// URL. |
Promise<void> — Promise resolving when the rename completes.TypeError— whenoldPathornewPathhas an invalid type.Error— withcode"ENOENT","EACCES","EINVAL", or"EIO".
copyFile
Section titled “copyFile”export function copyFile(src: PathInput, dest: PathInput, mode?: number): Promise<void>;
Asynchronously copy src to dest.
| Name | Type | Description |
|---|---|---|
src | PathInput | Source file path or file:// URL. |
dest | PathInput | Destination file path or file:// URL. |
mode optional | number | Optional bitmask (pass constants.COPYFILE_EXCL to fail with EEXIST if dest exists). |
Promise<void> — Promise resolving when the copy completes.TypeError— whensrcordesthas an invalid type.Error— withcode"ENOENT","EEXIST","EACCES","EINVAL", or"EIO".
realpath
Section titled “realpath”export function realpath(path: PathInput): Promise<string>;
Asynchronously compute the canonical pathname for path.
| Name | Type | Description |
|---|---|---|
path | PathInput | Path or file:// URL to canonicalize. |
Promise<string> — Promise resolving to the canonicalized path string.TypeError— whenpathhas an invalid type.Error— withcode"ENOENT","EACCES","EINVAL", or"EIO".
mkdtemp
Section titled “mkdtemp”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.
| Name | Type | Description |
|---|---|---|
prefix | string | Path prefix for the new temporary directory. |
Promise<string> — Promise resolving to the path of the created temporary directory.TypeError— whenprefixis not a string.Error— withcode"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 });export function open(
path: PathInput,
flags?: string | number,
mode?: string | number,
): Promise<FileHandle>;
Asynchronously open path and return a FileHandle.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
flags optional | string | number | Open flags string or numeric bitmask (default "r"). |
mode optional | string | number | POSIX file permissions when creating a file (default 0o666). |
Promise<FileHandle> — Promise resolving to an open FileHandle.access
Section titled “access”export function access(path: PathInput, mode?: number): Promise<void>;
Asynchronously test user permissions for path.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
mode optional | number | Optional accessibility bitmask (F_OK, R_OK, W_OK, X_OK, default F_OK). |
Promise<void> — Promise resolving if accessible, or rejecting on failure.symlink
Section titled “symlink”export function symlink(
target: PathInput,
path: PathInput,
type?: "dir" | "file" | "junction",
): Promise<void>;
Asynchronously create a symbolic link at path pointing to target.
| Name | Type | Description |
|---|---|---|
target | PathInput | Link target path. |
path | PathInput | Symbolic link path to create. |
type optional | "dir" | "file" | "junction" | Optional Windows symlink hint ("dir", "file", or "junction"). |
Promise<void> — Promise resolving when the symlink is created.readlink
Section titled “readlink”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.
| Name | Type | Description |
|---|---|---|
path | PathInput | Symbolic link path or file:// URL. |
options optional | TextEncoding | { encoding?: TextEncoding } | null | "buffer" | { encoding: "buffer" } | Optional encoding ("utf8" default); { encoding: "buffer" }. |
Promise<string>— Promise resolving to the symlink target string.Promise<Uint8Array>— Promise resolving to the symlink target asUint8Array.
export function chmod(path: PathInput, mode: string | number): Promise<void>;
Asynchronously change the permissions of path.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
mode | string | number | Numeric or octal string file mode. |
Promise<void> — Promise resolving when permissions are updated.export function cp(src: PathInput, dest: PathInput, options?: CpOptions): Promise<void>;
Asynchronously copy files or directories from src to dest.
| Name | Type | Description |
|---|---|---|
src | PathInput | Source file or directory path. |
dest | PathInput | Destination path. |
options optional | CpOptions | Optional copy behavior (recursive, force, filter, etc.). |
Promise<void> — Promise resolving when the copy completes.truncate
Section titled “truncate”export function truncate(path: PathInput, len?: number): Promise<void>;
Asynchronously truncate (or extend) the file at path to len bytes.
| Name | Type | Description |
|---|---|---|
path | PathInput | Filesystem path or file:// URL. |
len optional | number | Target byte length (default 0). |
Promise<void> — Promise resolving when truncation completes.export function watch(
filename: PathInput,
options?: WatchOptions | TextEncoding,
): AsyncIterableIterator<FileChangeInfo>;
Return an async iterator yielding filesystem change events for filename.
| Name | Type | Description |
|---|---|---|
filename | PathInput | File or directory path to watch. |
options optional | WatchOptions | TextEncoding | Optional watch settings or encoding string. |
AsyncIterableIterator<FileChangeInfo> — Async iterator of FileChangeInfo events.Interfaces
Section titled “Interfaces”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.
| Property | Type | Description | Since |
|---|---|---|---|
size readonly | number | Size of the file in bytes. | v0.0.0-alpha.0 |
mode readonly | number | POSIX file type and permissions bitmask. | v0.0.0-alpha.2 |
atimeMs readonly | number | Last access timestamp in milliseconds since the Unix epoch. | v0.0.0-alpha.2 |
atime readonly | Date | Last access timestamp as a Date instance. | v0.0.0-alpha.2 |
mtimeMs readonly | number | Last modification timestamp in milliseconds since the Unix epoch. | v0.0.0-alpha.0 |
mtime readonly | Date | Last modification timestamp as a Date instance. | v0.0.0-alpha.0 |
ctimeMs readonly | number | Last status change timestamp in milliseconds since the Unix epoch. | v0.0.0-alpha.2 |
ctime readonly | Date | Last status change timestamp as a Date instance. | v0.0.0-alpha.2 |
birthtimeMs readonly | number | File creation (birthtime) timestamp in milliseconds since the Unix epoch. | v0.0.0-alpha.2 |
birthtime readonly | Date | File creation (birthtime) timestamp as a Date instance. | v0.0.0-alpha.2 |
Stats.isFile
Section titled “Stats.isFile”isFile(): boolean;
Return true if the entry is a regular file.
boolean — true for a regular file, otherwise false.Stats.isDirectory
Section titled “Stats.isDirectory”isDirectory(): boolean;
Return true if the entry is a directory.
boolean — true for a directory, otherwise false.Stats.isSymbolicLink
Section titled “Stats.isSymbolicLink”isSymbolicLink(): boolean;
Return true if the entry is a symbolic link.
boolean — true for a symbolic link, otherwise false.Stats.isFIFO
Section titled “Stats.isFIFO”isFIFO(): boolean;
Return true if the entry is a FIFO (named pipe).
boolean — true for a FIFO, otherwise false.Stats.isSocket
Section titled “Stats.isSocket”isSocket(): boolean;
Return true if the entry is a socket.
boolean — true for a socket, otherwise false.Stats.isBlockDevice
Section titled “Stats.isBlockDevice”isBlockDevice(): boolean;
Return true if the entry is a block device.
boolean — true for a block device, otherwise false.Stats.isCharacterDevice
Section titled “Stats.isCharacterDevice”isCharacterDevice(): boolean;
Return true if the entry is a character device.
boolean — true for a character device, otherwise false.Dirent
Section titled “Dirent”export interface Dirent {
readonly name: string;
readonly parentPath: string;
isFile(): boolean;
isDirectory(): boolean;
isSymbolicLink(): boolean;
}
Directory entry returned by readdir with withFileTypes: true.
| Property | Type | Description | Since |
|---|---|---|---|
name readonly | string | Entry filename relative to the directory passed to readdir. | v0.0.0-alpha.0 |
parentPath readonly | string | Path to the parent directory containing this entry. | v0.0.0-alpha.2 |
Dirent.isFile
Section titled “Dirent.isFile”isFile(): boolean;
Return true if the entry is a regular file.
boolean — true for a regular file, otherwise false.Dirent.isDirectory
Section titled “Dirent.isDirectory”isDirectory(): boolean;
Return true if the entry is a directory.
boolean — true for a directory, otherwise false.Dirent.isSymbolicLink
Section titled “Dirent.isSymbolicLink”isSymbolicLink(): boolean;
Return true if the entry is a symbolic link.
boolean — true for a symbolic link, otherwise false.FsConstants
Section titled “FsConstants”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.
| Property | Type | Description | Since |
|---|---|---|---|
F_OK readonly | number | Flag testing existence of the path (0). | v0.0.0-alpha.0 |
R_OK readonly | number | Flag testing read permission (4). | v0.0.0-alpha.0 |
W_OK readonly | number | Flag testing write permission (2). | v0.0.0-alpha.0 |
X_OK readonly | number | Flag testing execute permission (1). | v0.0.0-alpha.0 |
COPYFILE_EXCL readonly | number | Copy flag failing if the destination file already exists (1). | v0.0.0-alpha.0 |
COPYFILE_FICLONE readonly | number | Copy flag requesting copy-on-write reflink if supported (2). | v0.0.0-alpha.0 |
COPYFILE_FICLONE_FORCE readonly | number | Copy flag requiring copy-on-write reflink (4). | v0.0.0-alpha.0 |
AbortSignalLike
Section titled “AbortSignalLike”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().
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback invoked on abort. |
options optional | { once?: boolean } | Optional listener options. |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback to remove. |
voidCpOptions
Section titled “CpOptions”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().
| Property | Type | Description | Since |
|---|---|---|---|
dereference optional | boolean | Dereference symbolic links during copy. | v0.0.0-alpha.2 |
errorOnExist optional | boolean | Throw 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 optional | boolean | Overwrite existing files (default true). | v0.0.0-alpha.2 |
recursive optional | boolean | Copy directories recursively (default false). | v0.0.0-alpha.2 |
preserveTimestamps optional | boolean | Preserve file timestamps when true. | v0.0.0-alpha.2 |
verbatimSymlinks optional | boolean | Preserve symlink behavior without following links. | v0.0.0-alpha.2 |
WatchOptions
Section titled “WatchOptions”export interface WatchOptions {
persistent?: boolean;
recursive?: boolean;
encoding?: TextEncoding | "buffer";
signal?: AbortSignalLike;
}
Options accepted by watch().
| Property | Type | Description | Since |
|---|---|---|---|
persistent optional | boolean | Keep the event loop active while watching (default true). | v0.0.0-alpha.2 |
recursive optional | boolean | Watch subdirectories recursively when true. | v0.0.0-alpha.2 |
encoding optional | TextEncoding | "buffer" | Character encoding for emitted filename strings. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal stopping the watcher. | v0.0.0-alpha.2 |
FileChangeInfo
Section titled “FileChangeInfo”export interface FileChangeInfo {
eventType: "rename" | "change";
filename: string | null;
}
Change event object yielded by fs/promises.watch().
| Property | Type | Description | Since |
|---|---|---|---|
eventType | "rename" | "change" | Filesystem change event kind ("rename" or "change"). | v0.0.0-alpha.2 |
filename | string | null | Changed filename relative to the watched path, or null. | v0.0.0-alpha.2 |
FileReadResult
Section titled “FileReadResult”export interface FileReadResult<T extends ArrayBufferView = ArrayBufferView> {
bytesRead: number;
buffer: T;
}
Result returned by FileHandle.read().
| Property | Type | Description | Since |
|---|---|---|---|
bytesRead | number | Number of bytes read into buffer. | v0.0.0-alpha.2 |
buffer | T | Target buffer populated by the read. | v0.0.0-alpha.2 |
FileWriteResult
Section titled “FileWriteResult”export interface FileWriteResult<T = ArrayBufferView | string> {
bytesWritten: number;
buffer: T;
}
Result returned by FileHandle.write().
| Property | Type | Description | Since |
|---|---|---|---|
bytesWritten | number | Number of bytes written. | v0.0.0-alpha.2 |
buffer | T | Source buffer or string passed to write(). | v0.0.0-alpha.2 |
FileHandle
Section titled “FileHandle”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().
| Property | Type | Description | Since |
|---|---|---|---|
fd readonly | number | Underlying numeric file descriptor. | v0.0.0-alpha.2 |
FileHandle.read
Section titled “FileHandle.read”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.
| Name | Type | Description |
|---|---|---|
buffer | T | — |
offset optional | number | null | — |
length optional | number | null | — |
position optional | number | null | — |
FileHandle.write
Section titled “FileHandle.write”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.
write(buffer, offset?, length?, position?)| Name | Type | Description |
|---|---|---|
buffer | T | — |
offset optional | number | null | — |
length optional | number | null | — |
position optional | number | null | — |
write(data, position?, encoding?)Write string data to the file handle.
| Name | Type | Description |
|---|---|---|
data | string | — |
position optional | number | null | — |
encoding optional | TextEncoding | — |
Promise<FileWriteResult<string>>FileHandle.readFile
Section titled “FileHandle.readFile”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.
| Name | Type | Description |
|---|---|---|
options optional | { encoding?: null | undefined } | null | TextEncoding | { encoding: TextEncoding } | — |
Promise<Uint8Array>Promise<string>
FileHandle.writeFile
Section titled “FileHandle.writeFile”writeFile(
data: string | Uint8Array | ArrayBufferView,
options?: TextEncoding | { encoding?: TextEncoding | null },
): Promise<void>;
Write data to the file handle.
| Name | Type | Description |
|---|---|---|
data | string | Uint8Array | ArrayBufferView | — |
options optional | TextEncoding | { encoding?: TextEncoding | null } | — |
Promise<void>FileHandle.appendFile
Section titled “FileHandle.appendFile”appendFile(
data: string | Uint8Array | ArrayBufferView,
options?: TextEncoding | { encoding?: TextEncoding | null },
): Promise<void>;
Append data to the file handle.
| Name | Type | Description |
|---|---|---|
data | string | Uint8Array | ArrayBufferView | — |
options optional | TextEncoding | { encoding?: TextEncoding | null } | — |
Promise<void>FileHandle.stat
Section titled “FileHandle.stat”stat(): Promise<Stats>;
Return Stats for the open file handle.
FileHandle.sync
Section titled “FileHandle.sync”sync(): Promise<void>;
Flush modified file data and metadata to disk.
Promise<void>FileHandle.datasync
Section titled “FileHandle.datasync”datasync(): Promise<void>;
Flush modified file data (without metadata) to disk.
Promise<void>FileHandle.truncate
Section titled “FileHandle.truncate”truncate(len?: number): Promise<void>;
Truncate or extend the file to len bytes (default 0).
| Name | Type | Description |
|---|---|---|
len optional | number | — |
Promise<void>FileHandle.chmod
Section titled “FileHandle.chmod”chmod(mode: string | number): Promise<void>;
Change file permissions on the open handle.
| Name | Type | Description |
|---|---|---|
mode | string | number | — |
Promise<void>FileHandle.close
Section titled “FileHandle.close”close(): Promise<void>;
Close the file handle.
Promise<void>FileHandle.[Symbol.asyncDispose]
Section titled “FileHandle.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the file handle automatically on await using.
Promise<void>Type Aliases
Section titled “Type Aliases”TextEncoding
Section titled “TextEncoding”export type TextEncoding = "utf8" | "utf-8" | "hex" | "base64" | "base64url" | "latin1" | "ascii";
Supported text encodings for filesystem string operations.
PathInput
Section titled “PathInput”export type PathInput = string | { readonly href: string; readonly protocol: string };
Filesystem path string or file:// URL-like object.
Constants
Section titled “Constants”constants
Section titled “constants”export const constants: FsConstants;
Filesystem access and copy mode constants.
Default Export
Section titled “Default Export”default export
Section titled “default export”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.
