Skip to main content

fs

@tauri-apps/api / fs

Module: fs

Access the file system.

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

The APIs must be allowlisted on 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
}
}
}
}

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.

Enumerations

Interfaces

References

Dir

Renames and re-exports BaseDirectory


writeFile

Renames and re-exports writeTextFile

Type Aliases

BinaryFileContents

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

Defined in

fs.ts:115

Functions

copyFile

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

Copys a file to a destination.

example Copy the $APPDIR/app.conf file to $APPDIR/app.conf.bk

import { copyFile, BaseDirectory } from '@tauri-apps/api/fs';
await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.App });

Parameters

NameTypeDescription
sourcestringA path of the file to copy.
destinationstringA path for the destination file.
optionsFsOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:458


createDir

createDir(dir, options?): 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 Create the $APPDIR/users directory

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

Parameters

NameTypeDescription
dirstringPath to the directory to create.
optionsFsDirOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:404


readBinaryFile

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

Reads a file as byte array.

example Read the image file in the $RESOURCEDIR/avatar.png path

import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });

Parameters

NameTypeDescription
filePathstringPath to the file.
optionsFsOptionsConfiguration object.

Returns

Promise<Uint8Array>

A promise resolving to the file bytes array.

Defined in

fs.ts:174


readDir

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

List directory files.

example Reads the $APPDIR/users directory recursively

import { readDir, BaseDirectory } from '@tauri-apps/api/fs';
const entries = await readDir('users', new Uint8Array([]), { dir: BaseDirectory.App, recursive: true });

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

Parameters

NameTypeDescription
dirstringPath to the directory to read.
optionsFsDirOptionsConfiguration object.

Returns

Promise<FileEntry[]>

A promise resolving to the directory entries.

Defined in

fs.ts:376


readTextFile

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

Reads a file as an UTF-8 encoded string.

example Read the text file in the $APPDIR/app.conf path

import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';
const contents = await readTextFile('app.conf', { dir: BaseDirectory.App });

Parameters

NameTypeDescription
filePathstringPath to the file.
optionsFsOptionsConfiguration object.

Returns

Promise<string>

A promise resolving to the file content as a UTF-8 encoded string.

Defined in

fs.ts:148


removeDir

removeDir(dir, options?): 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 Remove the directory $APPDIR/users

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

Parameters

NameTypeDescription
dirstringPath to the directory to remove.
optionsFsDirOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:431


removeFile

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

Removes a file.

example Remove the $APPDIR/app.conf file

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

Parameters

NameTypeDescription
filestringPath to the file to remove.
optionsFsOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:486


renameFile

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

Renames a file.

example Rename the $APPDIR/avatar.png file

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

Parameters

NameTypeDescription
oldPathstringA path of the file to rename.
newPathstringA path of the new file name.
optionsFsOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:513


writeBinaryFile

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

Writes a byte array content to a file.

example Write a binary file to the $APPDIR/avatar.png path

import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.App });

Parameters

NameTypeDescription
pathstringThe file path.
contentsBinaryFileContentsThe file contents.
options?FsOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:285

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

Writes a byte array content to a file.

example Write a binary file to the $APPEDIR/avatar.png path

import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.App });

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.

Defined in

fs.ts:303


writeTextFile

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

Writes a UTF-8 text file.

example Write a text file to the $APPDIR/app.conf path

import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.App });

Parameters

NameTypeDescription
pathstringThe file path.
contentsstringThe file contents.
options?FsOptionsConfiguration object.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:203

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

Writes a UTF-8 text file.

example Write a text file to the $APPDIR/app.conf path

import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.App });

Parameters

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

Returns

Promise<void>

A promise indicating the success or failure of the operation.

Defined in

fs.ts:221