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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | string | The data to Append. |
encodingName ? | string | The encoding to use. |
Returns
Promise
<void
>
Call Signature
function appendFile(
path: string,
data: string,
options: FileAppendOptions): Promise<void>;
Appends data to a file.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | string | The data to Append. |
options | FileAppendOptions | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The data to Append. |
options | FileAppendOptions | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The data to Append. |
options | FileAppendOptions | The options. |
Returns
Promise
<void
>
readFile()
Call Signature
function readFile(path: string): Promise<Uint8Array>;
Reads the entire content of a file.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path of the file. |
encodingName | string | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path of the file. |
options | FileReadOptions | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | string | The data to write. |
encodingName ? | string | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | string | The data to write. |
options | FileWriteOptions | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The data to write. |
options | FileWriteOptions | The 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
Parameter | Type | Description |
---|---|---|
path | string | The path to the file. |
data | Uint8Array | The data to write. |
options | FileWriteOptions | The 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.