Skip to main content

FileSystem module reference

The fs/promises module provides utilities for working with files and can be accessed using an import:

import { readFile } from 'fs/promises';

const content = await readFile('./foo.txt', 'utf8');

Functions

appendFile()

Call Signature

function appendFile(
path: string,
data: string,
encodingName?: string): Promise<void>;

Appends data to a file, replacing the file if it already exists.

Parameters
ParameterTypeDescription
pathstringThe path to the file.
datastringThe data to Append.
encodingName?stringThe encoding to use.
Returns

Promise<void>

Call Signature

function appendFile(
path: string,
data: string,
options: FileAppendOptions): Promise<void>;

Appends data to a file.

Parameters
ParameterTypeDescription
pathstringThe path to the file.
datastringThe data to Append.
optionsFileAppendOptionsThe options.
Returns

Promise<void>

Call Signature

function appendFile(path: string, data: Uint8Array): Promise<void>;

Appends data to a file, replacing the file if it already exists.

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to Append.
Returns

Promise<void>

Call Signature

function appendFile(
path: string,
data: Uint8Array,
options: FileAppendOptions): Promise<void>;

Appends data to a file, replacing the file if it already exists (depending on options.flag).

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to Append.
optionsFileAppendOptionsThe options.
Returns

Promise<void>

Call Signature

function appendFile(
path: string,
data: Uint8Array,
options: FileAppendOptions): Promise<void>;

Appends data to a file, replacing the file if it already exists (depending on options.flag).

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to Append.
optionsFileAppendOptionsThe options.
Returns

Promise<void>


readFile()

Call Signature

function readFile(path: string): Promise<Uint8Array>;

Reads the entire content of a file.

Parameters
ParameterTypeDescription
pathstringThe path of the file.
Returns

Promise<Uint8Array>

The data of the file.

Call Signature

function readFile(path: string, encodingName: string): Promise<string>;

Reads the entire content of a file.

Parameters
ParameterTypeDescription
pathstringThe path of the file.
encodingNamestringThe name of the encoding.
Returns

Promise<string>

The contents of the file.

Call Signature

function readFile(path: string, options: FileReadOptions): Promise<Uint8Array> | Promise<string>;

Reads the entire content of a file.

Parameters
ParameterTypeDescription
pathstringThe path of the file.
optionsFileReadOptionsThe options to read the file.
Returns

Promise<Uint8Array> | Promise<string>

The file content as string if an encoding is provided, as Uint8Array otherwise.


writeFile()

Call Signature

function writeFile(
path: string,
data: string,
encodingName?: string): Promise<void>;

Writes data to a file, replacing the file if it already exists.

Parameters
ParameterTypeDescription
pathstringThe path to the file.
datastringThe data to write.
encodingName?stringThe encoding to use.
Returns

Promise<void>

Call Signature

function writeFile(
path: string,
data: string,
options: FileWriteOptions): Promise<void>;

Writes data to a file, replacing the file if it already exists (depending on options.flag).

Parameters
ParameterTypeDescription
pathstringThe path to the file.
datastringThe data to write.
optionsFileWriteOptionsThe options.
Returns

Promise<void>

Call Signature

function writeFile(path: string, data: Uint8Array): Promise<void>;

Writes data to a file, replacing the file if it already exists.

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to write.
Returns

Promise<void>

Call Signature

function writeFile(
path: string,
data: Uint8Array,
options: FileWriteOptions): Promise<void>;

Writes data to a file, replacing the file if it already exists (depending on options.flag).

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to write.
optionsFileWriteOptionsThe options.
Returns

Promise<void>

Call Signature

function writeFile(
path: string,
data: Uint8Array,
options: FileWriteOptions): Promise<void>;

Writes data to a file, replacing the file if it already exists (depending on options.flag).

Parameters
ParameterTypeDescription
pathstringThe path to the file.
dataUint8ArrayThe data to write.
optionsFileWriteOptionsThe options.
Returns

Promise<void>

Type Aliases

FileAppendOptions

type FileAppendOptions = {
encoding: string;
flag: string;
};

Options to append to files.

Properties

encoding?
optional encoding: string;

The name of the encoding.

flag?
optional flag: string;

The file system flags.


FileReadOptions

type FileReadOptions = {
encoding: string;
};

Options when reading a file.

Properties

encoding?
optional encoding: string;

The encoding.


FileWriteOptions

type FileWriteOptions = {
encoding: string;
flag: string;
};

Options to write files.

Properties

encoding?
optional encoding: string;

The name of the encoding.

flag?
optional flag: string;

The file system flags.