Skip to main content

fs

Access the file system.

This package is also accessible with window.__TAURI__.fs when build.withGlobalTauri in tauri.conf.json is set to true.

The APIs must be added to tauri.allowlist.fs in tauri.conf.json:

{
"tauri": {
"allowlist": {
"fs": {
"all": true, // enable all FS APIs
"readFile": true,
"writeFile": true,
"readDir": true,
"copyFile": true,
"createDir": true,
"removeDir": true,
"removeFile": true,
"renameFile": true,
"exists": true
}
}
}
}

It is recommended to allowlist only the APIs you use for optimal bundle size and security.

Security​

This module prevents path traversal, not allowing absolute paths or parent dir components (i.e. "/usr/path/to/file" or "../path/to/file" paths are not allowed). Paths accessed with this API must be relative to one of the base directories so if you need access to arbitrary filesystem paths, you must write such logic on the core layer instead.

The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.

The scope configuration is an array of glob patterns describing folder paths that are allowed. For instance, this scope configuration only allows accessing files on the databases folder of the $APP directory:

{
"tauri": {
"allowlist": {
"fs": {
"scope": ["$APP/databases/*"]
}
}
}
}

Notice the use of the $APP variable. The value is injected at runtime, resolving to the app directory. The available variables are: $AUDIO, $CACHE, $CONFIG, $DATA, $LOCALDATA, $DESKTOP, $DOCUMENT, $DOWNLOAD, $EXE, $FONT, $HOME, $PICTURE, $PUBLIC, $RUNTIME, $TEMPLATE, $VIDEO, $RESOURCE, $APP, $LOG, $TEMP.

Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.

Note that this scope applies to all APIs on this module.

References​

Dir​

Renames and re-exports BaseDirectory

writeFile​

Renames and re-exports writeTextFile

Enumerations​

BaseDirectory​

Since: 1.0.0

Enumeration Members​

NameTypeDefined in
18fs.ts:96
1fs.ts:79
2fs.ts:80
3fs.ts:81
4fs.ts:82
6fs.ts:84
7fs.ts:85
8fs.ts:86
9fs.ts:87
10fs.ts:88
11fs.ts:89
5fs.ts:83
19fs.ts:97
12fs.ts:90
13fs.ts:91
17fs.ts:95
14fs.ts:92
20fs.ts:98
15fs.ts:93
16fs.ts:94

Interfaces​

FileEntry​

Since: 1.0.0

Properties​

children​

Optional children: FileEntry[]

Children of this entry if it's a directory; null otherwise

Defined in: fs.ts:154

name​

Optional name: string

Name of the directory/file can be null if the path terminates with ..

Defined in: fs.ts:152

path​

path: string

Defined in: fs.ts:147

FsBinaryFileOption​

Options object used to write a binary data to a file.

Since: 1.0.0

Properties​

contents​

contents: BinaryFileContents

The byte array contents.

Defined in: fs.ts:140

path​

path: string

Path to the file to write.

Defined in: fs.ts:138

FsDirOptions​

Since: 1.0.0

Properties​

dir​

Optional dir: BaseDirectory

Defined in: fs.ts:113

recursive​

Optional recursive: boolean

Defined in: fs.ts:114

FsOptions​

Since: 1.0.0

Properties​

dir​

Optional dir: BaseDirectory

Defined in: fs.ts:105

FsTextFileOption​

Options object used to write a UTF-8 string to a file.

Since: 1.0.0

Properties​

contents​

contents: string

The UTF-8 string to write to the file.

Defined in: fs.ts:126

path​

path: string

Path to the file to write.

Defined in: fs.ts:124

Type Aliases​

BinaryFileContents​

BinaryFileContents: Iterable<number> | ArrayLike<number> | ArrayBuffer

Defined in: fs.ts:129

Functions​

copyFile​

copyFile(source: string, destination: string, options?: FsOptions): Promise<void>

Copies a file to a destination.

Example

import { copyFile, BaseDirectory } from '@tauri-apps/api/fs';
// Copy the `$APPDIR/app.conf` file to `$APPDIR/app.conf.bk`
await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
sourcestring
destinationstring
optionsFsOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.

createDir​

createDir(dir: string, options?: FsDirOptions): Promise<void>

Creates a directory. If one of the path's parent components doesn't exist and the recursive option isn't set to true, the promise will be rejected.

Example

import { createDir, BaseDirectory } from '@tauri-apps/api/fs';
// Create the `$APPDIR/users` directory
await createDir('users', { dir: BaseDirectory.App, recursive: true });

Since: 1.0.0

Parameters

NameType
dirstring
optionsFsDirOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.

exists​

exists(path: string, options?: FsOptions): Promise<void>

Check if a path exists.

Example

import { exists, BaseDirectory } from '@tauri-apps/api/fs';
// Check if the `$APPDIR/avatar.png` file exists
await exists('avatar.png', { dir: BaseDirectory.App });

Since: 1.1.0

Parameters

NameType
pathstring
optionsFsOptions

Returns: Promise<void>

readBinaryFile​

readBinaryFile(filePath: string, options?: FsOptions): Promise<Uint8Array>

Reads a file as byte array.

Example

import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the image file in the `$RESOURCEDIR/avatar.png` path
const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });

Since: 1.0.0

Parameters

NameType
filePathstring
optionsFsOptions

Returns: Promise<Uint8Array>

readDir​

readDir(dir: string, options?: FsDirOptions): Promise<FileEntry[]>

List directory files.

Example

import { readDir, BaseDirectory } from '@tauri-apps/api/fs';
// Reads the `$APPDIR/users` directory recursively
const entries = await readDir('users', { dir: BaseDirectory.App, recursive: true });

function processEntries(entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.path}`);
if (entry.children) {
processEntries(entry.children)
}
}
}

Since: 1.0.0

Parameters

NameType
dirstring
optionsFsDirOptions

Returns: Promise<FileEntry[]>

readTextFile​

readTextFile(filePath: string, options?: FsOptions): Promise<string>

Reads a file as an UTF-8 encoded string.

Example

import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the text file in the `$APPDIR/app.conf` path
const contents = await readTextFile('app.conf', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
filePathstring
optionsFsOptions

Returns: Promise<string>

removeDir​

removeDir(dir: string, options?: FsDirOptions): Promise<void>

Removes a directory. If the directory is not empty and the recursive option isn't set to true, the promise will be rejected.

Example

import { removeDir, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the directory `$APPDIR/users`
await removeDir('users', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
dirstring
optionsFsDirOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.

removeFile​

removeFile(file: string, options?: FsOptions): Promise<void>

Removes a file.

Example

import { removeFile, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the `$APPDIR/app.conf` file
await removeFile('app.conf', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
filestring
optionsFsOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.

renameFile​

renameFile(oldPath: string, newPath: string, options?: FsOptions): Promise<void>

Renames a file.

Example

import { renameFile, BaseDirectory } from '@tauri-apps/api/fs';
// Rename the `$APPDIR/avatar.png` file
await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
oldPathstring
newPathstring
optionsFsOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.

writeBinaryFile​

writeBinaryFile(path: string, contents: BinaryFileContents, options?: FsOptions): Promise<void>

Writes a byte array content to a file.

Example

import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a binary file to the `$APPDIR/avatar.png` path
await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameTypeDescription
pathstring-
contentsBinaryFileContents-
options?FsOptionsConfiguration object.

Returns: Promise<void>

A promise indicating the success or failure of the operation.

writeBinaryFile(file: FsBinaryFileOption, options?: FsOptions): Promise<void>

Writes a byte array content to a file.

Example

import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a binary file to the `$APPDIR/avatar.png` path
await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameTypeDescription
fileFsBinaryFileOptionThe object containing the file path and contents.
options?FsOptionsConfiguration object.

Returns: Promise<void>

A promise indicating the success or failure of the operation.

writeTextFile​

writeTextFile(path: string, contents: string, options?: FsOptions): Promise<void>

Writes a UTF-8 text file.

Example

import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a text file to the `$APPDIR/app.conf` path
await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
pathstring
contentsstring
options?FsOptions

Returns: Promise<void>

writeTextFile(file: FsTextFileOption, options?: FsOptions): Promise<void>

Writes a UTF-8 text file.

Example

import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a text file to the `$APPDIR/app.conf` path
await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.App });

Since: 1.0.0

Parameters

NameType
fileFsTextFileOption
options?FsOptions

Returns: Promise<void>

A promise indicating the success or failure of the operation.