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');
Path resolution
All fs/promises paths are resolved relative to the directory of the running operation or script (equivalent to using path.resolve from that directory).
Project sandbox
Kreya scripts can only access files within the Kreya project directory and its subdirectories; access outside this sandbox is blocked.
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 (resolved relative to the directory of the running script/operation). |
data | string | The data to append. |
encodingName? | string | The encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url. |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
data | Uint8Array | The data to append. |
options | FileAppendOptions | The options. |
Returns
Promise<void>
readFile()
Call Signature
function readFile(path: string): Promise<Uint8Array<ArrayBufferLike>>;
Reads the entire content of a file.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The path to the file (resolved relative to the directory of the running script/operation). |
Returns
Promise<Uint8Array<ArrayBufferLike>>
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 to the file (resolved relative to the directory of the running script/operation). |
encodingName | string | The encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url. |
Returns
Promise<string>
The contents of the file.
Call Signature
function readFile(path: string, options: FileReadOptions): Promise<Uint8Array<ArrayBufferLike>> | Promise<string>;
Reads the entire content of a file.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The path of the file (resolved relative to the running script). |
options | FileReadOptions | The options to read the file, including encoding. |
Returns
Promise<Uint8Array<ArrayBufferLike>> | 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 (resolved relative to the directory of the running script/operation). |
data | string | The data to write. |
encodingName? | string | The encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url. |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
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 (resolved relative to the directory of the running script/operation). |
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 encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url.
flag?
optional flag: string;
File system flag (default a). Supported values: a, ax, a+, ax+, as, as+, w, wx, w+, wx+, r, rs, r+, rs+. Use an x suffix to fail if the file exists, + to allow reading, and s to open synchronously. See also https://nodejs.org/api/fs.html#file-system-flags.
FileReadOptions
type FileReadOptions = {
encoding?: string;
};
Options when reading a file.
Properties
encoding?
optional encoding: string;
The encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url.
FileWriteOptions
type FileWriteOptions = {
encoding?: string;
flag?: string;
};
Options to write files.
Properties
encoding?
optional encoding: string;
The encoding to use: utf8 (default), utf16le, ascii, hex, base64, or base64url.
flag?
optional flag: string;
File system flag (default w). Supported values: w, wx, w+, wx+, a, ax, a+, ax+, as, as+, r, rs, r+, rs+. Use an x suffix to fail if the file exists, + to allow reading, and s to open synchronously. See also https://nodejs.org/api/fs.html#file-system-flags.