Skip to content

@tauri-apps/plugin-fs

Access the file system.

On iOS, the fs plugin automatically manages access to security-scoped resources when a file URL is accessed. This is required for files outside the app’s sandbox (e.g., from file picker).

import { open } from '@tauri-apps/plugin-fs';
const file = await open('file:///path/to/file.txt');
await file.close();

This module prevents path traversal, not allowing parent directory accessors to be used (i.e. “/usr/path/to/../file” or “../path/to/file” paths are not allowed). Paths accessed with this API must be either relative to one of the base directories or created with the path API.

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 file/directory paths that are allowed. For instance, this scope configuration allows all enabled fs APIs to (only) access files in the databases directory of the $APPDATA directory:

{
"permissions": [
{
"identifier": "fs:scope",
"allow": [{ "path": "$APPDATA/databases/*" }]
}
]
}

Scopes can also be applied to specific fs APIs by using the API’s identifier instead of fs:scope:

{
"permissions": [
{
"identifier": "fs:allow-exists",
"allow": [{ "path": "$APPDATA/databases/*" }]
}
]
}

Notice the use of the $APPDATA variable. The value is injected at runtime, resolving to the app data directory.

The available variables are: $APPCONFIG, $APPDATA, $APPLOCALDATA, $APPCACHE, $APPLOG, $AUDIO, $CACHE, $CONFIG, $DATA, $LOCALDATA, $DESKTOP, $DOCUMENT, $DOWNLOAD, $EXE, $FONT, $HOME, $PICTURE, $PUBLIC, $RUNTIME, $TEMPLATE, $VIDEO, $RESOURCE, $TEMP.

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

function copyFile(
fromPath: string | URL,
toPath: string | URL,
options?: CopyFileOptions
): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:644

Copies the contents and permissions of one file to another specified path, by default creating a new file if needed, else overwriting.

import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });
Parameter Type Description
fromPath string | URL The path of the file to copy from.
toPath string | URL The path of the file to copy to.
options? CopyFileOptions Options defining the base directory of each path.

Promise<void>

2.0.0


function create(path: string | URL, options?: CreateOptions): Promise<FileHandle>;

Defined in: plugins/fs/guest-js/index.ts:513

Creates a file if none exists or truncates an existing file and resolves to an instance of FileHandle.

import { create, BaseDirectory } from "@tauri-apps/plugin-fs"
const file = await create("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
await file.write(new TextEncoder().encode("Hello world"));
await file.close();
Parameter Type Description
path string | URL The path of the file, relative to options.baseDir when it is provided.
options? CreateOptions Options defining the base directory of path.

Promise<FileHandle>

A promise resolving to the handle of the created file.

2.0.0


function exists(path: string | URL, options?: ExistsOptions): Promise<boolean>;

Defined in: plugins/fs/guest-js/index.ts:1280

Check if a path exists.

import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
// Check if the `$APPDATA/avatar.png` file exists
await exists('avatar.png', { baseDir: BaseDirectory.AppData });
Parameter Type Description
path string | URL The path to check.
options? ExistsOptions Options defining the base directory of path.

Promise<boolean>

A promise resolving to true when the path exists, false otherwise.

2.0.0


function lstat(path: string | URL, options?: StatOptions): Promise<FileInfo>;

Defined in: plugins/fs/guest-js/index.ts:1087

Resolves to a FileInfo for the specified path. If path is a symlink, information for the symlink will be returned instead of what it points to.

import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';
const fileInfo = await lstat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
console.log(fileInfo.isFile); // true
Parameter Type Description
path string | URL The path of the file, directory or symlink to inspect.
options? StatOptions Options defining the base directory of path.

Promise<FileInfo>

A promise resolving to the metadata of the path itself.

2.0.0


function mkdir(path: string | URL, options?: MkdirOptions): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:691

Creates a new directory with the specified path.

import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
await mkdir('users', { baseDir: BaseDirectory.AppLocalData });
Parameter Type Description
path string | URL The path of the directory to create.
options? MkdirOptions Options defining the base directory of path, the directory permissions and whether intermediate directories are created.

Promise<void>

2.0.0


function open(path: string | URL, options?: OpenOptions): Promise<FileHandle>;

Defined in: plugins/fs/guest-js/index.ts:603

Open a file and resolve to an instance of FileHandle. The file does not need to previously exist if using the create or createNew open options. It is the callers responsibility to close the file when finished with it.

import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
const file = await open("foo/bar.txt", { read: true, write: true, baseDir: BaseDirectory.AppLocalData });
// Do work with file
await file.close();
Parameter Type Description
path string | URL The path of the file, relative to options.baseDir when it is provided.
options? OpenOptions Options defining the base directory of path and how the file is opened.

Promise<FileHandle>

A promise resolving to the handle of the open file.

2.0.0


function readDir(path: string | URL, options?: ReadDirOptions): Promise<DirEntry[]>;

Defined in: plugins/fs/guest-js/index.ts:758

Reads the directory given by path and returns an array of DirEntry.

import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
import { join } from '@tauri-apps/api/path';
const dir = 'users';
const entries = await readDir(dir, { baseDir: BaseDirectory.AppLocalData });
await processEntriesRecursively(dir, entries);
async function processEntriesRecursively(parent, entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.name}`);
if (entry.isDirectory) {
const entryPath = await join(parent, entry.name);
await processEntriesRecursively(entryPath, await readDir(entryPath, { baseDir: BaseDirectory.AppLocalData }));
}
}
}
Parameter Type Description
path string | URL The path of the directory to read.
options? ReadDirOptions Options defining the base directory of path.

Promise<DirEntry[]>

A promise resolving to the list of entries in the directory.

2.0.0


function readFile(path: string | URL, options?: ReadFileOptions): Promise<Uint8Array>;

Defined in: plugins/fs/guest-js/index.ts:798

Reads and resolves to the entire contents of a file as an array of bytes. TextDecoder can be used to transform the bytes to string if required.

import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';
const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });
Parameter Type Description
path string | URL The path of the file to read.
options? ReadFileOptions Options defining the base directory of path.

Promise<Uint8Array>

A promise resolving to the contents of the file as bytes.

2.0.0


function readTextFile(path: string | URL, options?: ReadFileOptions): Promise<string>;

Defined in: plugins/fs/guest-js/index.ts:827

Reads and returns the entire contents of a file as a string using the specified encoding (default: UTF-8).

import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });
Parameter Type Description
path string | URL The path of the file to read.
options? ReadFileOptions Options defining the base directory of path and the text encoding.

Promise<string>

A promise resolving to the contents of the file as a string.

2.0.0


function readTextFileLines(path: string | URL, options?: ReadFileOptions): Promise<AsyncIterableIterator<string>>;

Defined in: plugins/fs/guest-js/index.ts:863

Returns an async AsyncIterableIterator over the lines of a file, decoded using the specified encoding (default: UTF-8).

import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
const lines = await readTextFileLines('app.conf', { baseDir: BaseDirectory.AppConfig });
for await (const line of lines) {
console.log(line);
}

You could also call AsyncIterableIterator.next to advance the iterator so you can lazily read the next line whenever you want.

Parameter Type Description
path string | URL The path of the file to read.
options? ReadFileOptions Options defining the base directory of path and the text encoding.

Promise<AsyncIterableIterator<string>>

A promise resolving to an iterator over the lines of the file.

2.0.0


function remove(path: string | URL, options?: RemoveOptions): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:969

Removes the named file or directory. If the directory is not empty and the recursive option isn’t set to true, the promise will be rejected.

import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';
await remove('users/file.txt', { baseDir: BaseDirectory.AppLocalData });
await remove('users', { baseDir: BaseDirectory.AppLocalData });
Parameter Type Description
path string | URL The path of the file or directory to remove.
options? RemoveOptions Options defining the base directory of path and whether directories are removed recursively.

Promise<void>

2.0.0


function rename(
oldPath: string | URL,
newPath: string | URL,
options?: RenameOptions
): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1013

Renames (moves) oldpath to newpath. Paths may be files or directories. If newpath already exists and is not a directory, rename() replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories.

On Unix, this operation does not follow symlinks at either path.

import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';
await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
Parameter Type Description
oldPath string | URL The path of the file or directory to rename.
newPath string | URL The path the file or directory is renamed to.
options? RenameOptions Options defining the base directory of each path.

Promise<void>

2.0.0


function size(path: string | URL, options?: SizeOptions): Promise<number>;

Defined in: plugins/fs/guest-js/index.ts:1544

Get the size of a file or directory. For files, the stat functions can be used as well.

If path is a directory, this function will recursively iterate over every file and every directory inside of path and therefore will be very time consuming if used on larger directories.

import { size, BaseDirectory } from '@tauri-apps/plugin-fs';
// Get the size of the `$APPDATA/tauri` directory.
const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData });
console.log(dirSize); // 1024
Parameter Type Description
path string | URL The path of the file or directory to measure.
options? SizeOptions Options defining the base directory of path (since 2.6.0).

Promise<number>

A promise resolving to the size in bytes.

2.1.0


function startAccessingSecurityScopedResource(path: string | URL): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1585

Starts accessing a security-scoped resource for the given file URL. This should be called when you’re accessing a file that was opened using a security-scoped URL (e.g., from a file picker).

Note that accessing security-scoped resources is automatically managed by the plugin on iOS, so you don’t need to call this function unless you want to manage the scope manually.

You must call stopAccessingSecurityScopedResource when you’re done accessing the resource.

Platform-specific

  • iOS: Starts accessing the security-scoped resource.
  • Other platforms: does nothing.
import { startAccessingSecurityScopedResource } from '@tauri-apps/plugin-fs';
const filePath = 'file:///path/to/file.txt';
await startAccessingSecurityScopedResource(filePath);
// ... use the resource ...
Parameter Type Description
path string | URL The path or file:// URL of the resource to start accessing.

Promise<void>

2.5.0


function stat(path: string | URL, options?: StatOptions): Promise<FileInfo>;

Defined in: plugins/fs/guest-js/index.ts:1058

Resolves to a FileInfo for the specified path. Will always follow symlinks but will reject if the symlink points to a path outside of the scope.

import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';
const fileInfo = await stat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
console.log(fileInfo.isFile); // true
Parameter Type Description
path string | URL The path of the file or directory to inspect.
options? StatOptions Options defining the base directory of path.

Promise<FileInfo>

A promise resolving to the metadata of the file or directory.

2.0.0


function stopAccessingSecurityScopedResource(path: string | URL): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1621

Stops accessing a security-scoped resource for the given file URL. This should be called when you’re done accessing a file that was opened using a security-scoped URL (e.g., from a file picker) when using manual tracking via startAccessingSecurityScopedResource.

Platform-specific

  • iOS: Stops accessing the security-scoped resource.
  • Other platforms: does nothing.
import { stopAccessingSecurityScopedResource } from '@tauri-apps/plugin-fs';
const filePath = 'file:///path/to/file.txt';
await startAccessingSecurityScopedResource(filePath);
// ... use the resource ...
// when you're done with the resource:
await stopAccessingSecurityScopedResource(filePath);
Parameter Type Description
path string | URL The path or file:// URL of the resource to stop accessing.

Promise<void>

2.5.0


function truncate(
path: string | URL,
len?: number,
options?: TruncateOptions
): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1132

Truncates or extends the specified file, to reach the specified len. If len is 0 or not specified, then the entire file contents are truncated.

import { truncate, readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire file
await truncate("my_file.txt", 0, { baseDir: BaseDirectory.AppLocalData });
// truncate part of the file
const filePath = "file.txt";
await writeTextFile(filePath, "Hello World", { baseDir: BaseDirectory.AppLocalData });
await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData });
const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData });
console.log(data); // "Hello W"
Parameter Type Description
path string | URL The path of the file to truncate or extend.
len? number The length the file is resized to, in bytes. Defaults to 0.
options? TruncateOptions Options defining the base directory of path.

Promise<void>

2.0.0


function watch(
paths: string | string[] | URL | URL[],
cb: (event: WatchEvent) => void,
options?: DebouncedWatchOptions
): Promise<UnwatchFn>;

Defined in: plugins/fs/guest-js/index.ts:1467

Watch changes (after a delay) on files or directories.

Events that happen within the delayMs window are grouped and delivered in a single callback call. Requires the watch Cargo feature of the Rust plugin to be enabled.

import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';
const unwatch = await watch(
'app.conf',
(event) => console.log(event.type, event.paths),
{ baseDir: BaseDirectory.AppConfig, delayMs: 500 }
);
// stop watching when you are done
unwatch();
Parameter Type Description
paths | string | string[] | URL | URL[] The path or list of paths to watch. Each path can be a string or a file:// URL.
cb (event: WatchEvent) => void The callback executed for each batch of file system changes.
options? DebouncedWatchOptions Options defining the base directory of the paths, the debounce delay and whether directories are watched recursively.

Promise<UnwatchFn>

A promise resolving to a function that stops watching the given paths.

2.0.0


function watchImmediate(
paths: string | string[] | URL | URL[],
cb: (event: WatchEvent) => void,
options?: WatchOptions
): Promise<UnwatchFn>;

Defined in: plugins/fs/guest-js/index.ts:1505

Watch changes on files or directories.

Unlike watch, changes are reported as soon as they are detected, without a debounce delay. Requires the watch Cargo feature of the Rust plugin to be enabled.

import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';
const unwatch = await watchImmediate(
'logs',
(event) => console.log(event.type, event.paths),
{ baseDir: BaseDirectory.AppLog, recursive: true }
);
// stop watching when you are done
unwatch();
Parameter Type Description
paths | string | string[] | URL | URL[] The path or list of paths to watch. Each path can be a string or a file:// URL.
cb (event: WatchEvent) => void The callback executed for each file system change.
options? WatchOptions Options defining the base directory of the paths and whether directories are watched recursively.

Promise<UnwatchFn>

A promise resolving to a function that stops watching the given paths.

2.0.0


function writeFile(
path: string | URL,
data: Uint8Array | ReadableStream<Uint8Array>,
options?: WriteFileOptions
): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1182

Write data to the given path, by default creating a new file if needed, else overwriting.

import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
let encoder = new TextEncoder();
let data = encoder.encode("Hello World");
await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
Parameter Type Description
path string | URL The path of the file to write to.
data | Uint8Array | ReadableStream<Uint8Array> The bytes written to the file, either as a buffer or as a stream of chunks.
options? WriteFileOptions Options defining the base directory of path and how the file is opened.

Promise<void>

2.0.0


function writeTextFile(
path: string | URL,
data: string,
options?: WriteFileOptions
): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:1237

Writes UTF-8 string data to the given path, by default creating a new file if needed, else overwriting.

import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
Parameter Type Description
path string | URL The path of the file to write to.
data string The UTF-8 string written to the file.
options? WriteFileOptions Options defining the base directory of path and how the file is opened.

Promise<void>

2.0.0

Defined in: @tauri-apps/api/path.d.ts:4

2.0.0

Enumeration Member Value Description Defined in
AppCache 16 See appCacheDir for more information. @tauri-apps/api/path.d.ts:73
AppConfig 13 See appConfigDir for more information. @tauri-apps/api/path.d.ts:61
AppData 14 See appDataDir for more information. @tauri-apps/api/path.d.ts:65
AppLocalData 15 See appLocalDataDir for more information. @tauri-apps/api/path.d.ts:69
AppLog 17 See appLogDir for more information. @tauri-apps/api/path.d.ts:77
Audio 1 See audioDir for more information. @tauri-apps/api/path.d.ts:8
Cache 2 See cacheDir for more information. @tauri-apps/api/path.d.ts:12
Config 3 See configDir for more information. @tauri-apps/api/path.d.ts:16
Data 4 See dataDir for more information. @tauri-apps/api/path.d.ts:20
Desktop 18 See desktopDir for more information. Platform-specific - Android: Unsupported, this variant does not exist on Android. @tauri-apps/api/path.d.ts:85
Document 6 See documentDir for more information. @tauri-apps/api/path.d.ts:28
Download 7 See downloadDir for more information. @tauri-apps/api/path.d.ts:32
Executable 19 See executableDir for more information. Platform-specific - Android: Unsupported, this variant does not exist on Android. @tauri-apps/api/path.d.ts:93
Font 20 See fontDir for more information. Platform-specific - Android: Unsupported, this variant does not exist on Android. @tauri-apps/api/path.d.ts:101
Home 21 See homeDir for more information. @tauri-apps/api/path.d.ts:105
LocalData 5 See localDataDir for more information. @tauri-apps/api/path.d.ts:24
Picture 8 See pictureDir for more information. @tauri-apps/api/path.d.ts:36
Public 9 See publicDir for more information. @tauri-apps/api/path.d.ts:40
Resource 11 See resourceDir for more information. @tauri-apps/api/path.d.ts:53
Runtime 22 See runtimeDir for more information. Platform-specific - Android: Unsupported, this variant does not exist on Android. @tauri-apps/api/path.d.ts:113
Temp 12 See tempDir for more information. @tauri-apps/api/path.d.ts:57
Template 23 See templateDir for more information. Platform-specific - Android: Unsupported, this variant does not exist on Android. @tauri-apps/api/path.d.ts:121
Video 10 See videoDir for more information. Platform-specific - Android: Resolves to the app-specific Movies directory since 2.12.0, see videoDir. @tauri-apps/api/path.d.ts:49

Defined in: plugins/fs/guest-js/index.ts:94

Defines how the offset given to FileHandle.seek is interpreted.

Enumeration Member Value Description Defined in
Current 1 The offset is relative to the current cursor position. plugins/fs/guest-js/index.ts:98
End 2 The offset is relative to the end of the file. plugins/fs/guest-js/index.ts:100
Start 0 The offset is relative to the start of the file. plugins/fs/guest-js/index.ts:96

Defined in: plugins/fs/guest-js/index.ts:301

The Tauri abstraction for reading and writing files.

2.0.0

  • Resource
[key: number]: () => Promise<void>

new FileHandle(rid: number): FileHandle;

Defined in: @tauri-apps/api/core.d.ts:413

Parameter Type
rid number

FileHandle

Resource.constructor

get rid(): number;

Defined in: @tauri-apps/api/core.d.ts:412

number

Resource.rid

close(): Promise<void>;

Defined in: @tauri-apps/api/core.d.ts:421

Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.

Promise<void>

Uses the core:resources:allow-close permission, which is part of the core:resources:default permission set enabled by default.

Resource.close

read(buffer: Uint8Array): Promise<number | null>;

Defined in: plugins/fs/guest-js/index.ts:335

Reads up to p.byteLength bytes into p. It resolves to the number of bytes read (0 < n <= p.byteLength) and rejects if any error encountered. Even if read() resolves to n < p.byteLength, it may use all of p as scratch space during the call. If some data is available but not p.byteLength bytes, read() conventionally resolves to what is available instead of waiting for more.

When read() encounters end-of-file condition, it resolves to EOF (null).

When read() encounters an error, it rejects with an error.

Callers should always process the n > 0 bytes returned before considering the EOF (null). Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
// if "$APPCONFIG/foo/bar.txt" contains the text "hello world":
const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
const buf = new Uint8Array(100);
const numberOfBytesRead = await file.read(buf); // 11 bytes
const text = new TextDecoder().decode(buf); // "hello world"
await file.close();
Parameter Type Description
buffer Uint8Array The buffer the file contents are read into.

Promise<number | null>

A promise resolving to the number of bytes read, or null when the end of the file was reached.

2.0.0

seek(offset: number, whence: SeekMode): Promise<number>;

Defined in: plugins/fs/guest-js/index.ts:393

Seek sets the offset for the next read() or write() to offset, interpreted according to whence: Start means relative to the start of the file, Current means relative to the current offset, and End means relative to the end. Seek resolves to the new offset relative to the start of the file.

Seeking to an offset before the start of the file is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent. It returns the number of cursor position.

import { open, SeekMode, BaseDirectory } from '@tauri-apps/plugin-fs';
// Given hello.txt pointing to file with "Hello world", which is 11 bytes long:
const file = await open('hello.txt', { read: true, write: true, truncate: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.write(new TextEncoder().encode("Hello world"));
// Seek 6 bytes from the start of the file
console.log(await file.seek(6, SeekMode.Start)); // "6"
// Seek 2 more bytes from the current position
console.log(await file.seek(2, SeekMode.Current)); // "8"
// Seek backwards 2 bytes from the end of the file
console.log(await file.seek(-2, SeekMode.End)); // "9" (e.g. 11-2)
await file.close();
Parameter Type Description
offset number The number of bytes the cursor is moved by.
whence SeekMode Defines the position the offset is relative to.

Promise<number>

A promise resolving to the new cursor position, relative to the start of the file.

2.0.0

stat(): Promise<FileInfo>;

Defined in: plugins/fs/guest-js/index.ts:416

Returns a FileInfo for this file.

import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
const file = await open("file.txt", { read: true, baseDir: BaseDirectory.AppLocalData });
const fileInfo = await file.stat();
console.log(fileInfo.isFile); // true
await file.close();

Promise<FileInfo>

A promise resolving to the metadata of this file.

2.0.0

truncate(len?: number): Promise<void>;

Defined in: plugins/fs/guest-js/index.ts:449

Truncates or extends this file, to reach the specified len. If len is not specified then the entire file contents are truncated.

import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire file
const file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.truncate();
// truncate part of the file
const file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.write(new TextEncoder().encode("Hello World"));
await file.truncate(7);
const data = new Uint8Array(32);
await file.read(data);
console.log(new TextDecoder().decode(data)); // Hello W
await file.close();
Parameter Type Description
len? number The length the file is truncated or extended to, in bytes. When not provided the entire file contents are truncated.

Promise<void>

2.0.0

write(data: Uint8Array): Promise<number>;

Defined in: plugins/fs/guest-js/index.ts:478

Writes data.byteLength bytes from data to the underlying data stream. It resolves to the number of bytes written from data (0 <= n <= data.byteLength) or reject with the error encountered that caused the write to stop early. write() must reject with a non-null error if would resolve to n < data.byteLength. write() must not modify the slice data, even temporarily.

import { open, write, BaseDirectory } from '@tauri-apps/plugin-fs';
const encoder = new TextEncoder();
const data = encoder.encode("Hello world");
const file = await open("bar.txt", { write: true, baseDir: BaseDirectory.AppLocalData });
const bytesWritten = await file.write(data); // 11
await file.close();
Parameter Type Description
data Uint8Array The bytes written to the file.

Promise<number>

A promise resolving to the number of bytes written.

2.0.0

Defined in: plugins/fs/guest-js/index.ts:624

Options for the copyFile function, defining the base directory of each path.

2.0.0

Property Type Description Defined in
fromPathBaseDir? BaseDirectory Base directory for fromPath. plugins/fs/guest-js/index.ts:626
toPathBaseDir? BaseDirectory Base directory for toPath. plugins/fs/guest-js/index.ts:628

Defined in: plugins/fs/guest-js/index.ts:491

Options for the create function, which creates or truncates a file.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:493

Defined in: plugins/fs/guest-js/index.ts:1311

Options for the watch function, which reports file system changes after a debounce delay.

2.0.0

Property Type Description Inherited from Defined in
baseDir? BaseDirectory Base directory for path WatchOptions.baseDir plugins/fs/guest-js/index.ts:1303
delayMs? number The debounce delay in milliseconds. Changes that happen within this window are grouped and reported together. Defaults to 2000. - plugins/fs/guest-js/index.ts:1316
recursive? boolean Watch a directory recursively WatchOptions.recursive plugins/fs/guest-js/index.ts:1301

Defined in: plugins/fs/guest-js/index.ts:722

A disk entry which is either a file, a directory or a symlink.

This is the result of the readDir.

2.0.0

Property Type Description Defined in
isDirectory boolean Specifies whether this entry is a directory or not. plugins/fs/guest-js/index.ts:726
isFile boolean Specifies whether this entry is a file or not. plugins/fs/guest-js/index.ts:728
isSymlink boolean Specifies whether this entry is a symlink or not. plugins/fs/guest-js/index.ts:730
name string The name of the entry (file name with extension or directory name). plugins/fs/guest-js/index.ts:724

Defined in: plugins/fs/guest-js/index.ts:1261

Options for the exists function, which checks whether a path exists.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path. plugins/fs/guest-js/index.ts:1263

Defined in: plugins/fs/guest-js/index.ts:108

A FileInfo describes a file and is returned by stat, lstat or fstat.

2.0.0

Property Type Description Defined in
atime | Date | null The last access time of the file. This corresponds to the atime field from stat on Unix and ftLastAccessTime on Windows. This may not be available on all platforms. plugins/fs/guest-js/index.ts:139
birthtime | Date | null The creation time of the file. This corresponds to the birthtime field from stat on Mac/BSD and ftCreationTime on Windows. This may not be available on all platforms. plugins/fs/guest-js/index.ts:145
blksize number | null Blocksize for filesystem I/O. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:222
blocks number | null Number of blocks allocated to the file, in 512-byte units. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:230
dev number | null ID of the device containing the file. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:165
fileAttributes number | null This field contains the file system attribute information for a file or directory. For possible values and their descriptions, see File Attribute Constants in the Windows Dev Center Platform-specific - macOS / Linux / Android / iOS: Unsupported. plugins/fs/guest-js/index.ts:157
gid number | null Group ID of the owner of this file. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:206
ino number | null Inode number. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:173
isDirectory boolean True if this is info for a regular directory. Mutually exclusive to FileInfo.isFile and FileInfo.isSymlink. plugins/fs/guest-js/index.ts:118
isFile boolean True if this is info for a regular file. Mutually exclusive to FileInfo.isDirectory and FileInfo.isSymlink. plugins/fs/guest-js/index.ts:113
isSymlink boolean True if this is info for a symlink. Mutually exclusive to FileInfo.isFile and FileInfo.isDirectory. plugins/fs/guest-js/index.ts:123
mode number | null The underlying raw st_mode bits that contain the standard Unix permissions for this file/directory. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:182
mtime | Date | null The last modification time of the file. This corresponds to the mtime field from stat on Linux/Mac OS and ftLastWriteTime on Windows. This may not be available on all platforms. plugins/fs/guest-js/index.ts:133
nlink number | null Number of hard links pointing to this file. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:190
rdev number | null Device ID of this file. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:214
readonly boolean Whether this is a readonly (unwritable) file. plugins/fs/guest-js/index.ts:147
size number The size of the file, in bytes. plugins/fs/guest-js/index.ts:127
uid number | null User ID of the owner of this file. Platform-specific - Windows: Unsupported. plugins/fs/guest-js/index.ts:198

Defined in: plugins/fs/guest-js/index.ts:668

Options for the mkdir function, which creates a directory.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:676
mode? number Permissions to use when creating the directory (defaults to 0o777, before the process’s umask). Ignored on Windows. plugins/fs/guest-js/index.ts:670
recursive? boolean Defaults to false. If set to true, means that any intermediate directories will also be created (as with the shell command mkdir -p). plugins/fs/guest-js/index.ts:674

Defined in: plugins/fs/guest-js/index.ts:534

Options for the open function, defining how the file is opened and which operations are allowed on it.

2.0.0

Property Type Description Defined in
append? boolean Sets the option for the append mode. This option, when true, means that writes will append to a file instead of overwriting previous contents. Note that setting { write: true, append: true } has the same effect as setting only { append: true }. plugins/fs/guest-js/index.ts:553
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:581
create? boolean Sets the option to allow creating a new file, if one doesn’t already exist at the specified path. Requires write or append access to be used. plugins/fs/guest-js/index.ts:566
createNew? boolean Defaults to false. If set to true, no file, directory, or symlink is allowed to exist at the target location. Requires write or append access to be used. When createNew is set to true, create and truncate are ignored. plugins/fs/guest-js/index.ts:573
mode? number Permissions to use if creating the file (defaults to 0o666, before the process’s umask). Ignored on Windows. plugins/fs/guest-js/index.ts:579
read? boolean Sets the option for read access. This option, when true, means that the file should be read-able if opened. plugins/fs/guest-js/index.ts:539
truncate? boolean Sets the option for truncating a previous file. If a file is successfully opened with this option set it will truncate the file to 0 size if it already exists. The file must be opened with write access for truncate to work. plugins/fs/guest-js/index.ts:560
write? boolean Sets the option for write access. This option, when true, means that the file should be write-able if opened. If the file already exists, any write calls on it will overwrite its contents, by default without truncating it. plugins/fs/guest-js/index.ts:546

Defined in: plugins/fs/guest-js/index.ts:710

Options for the readDir function, which lists the entries of a directory.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:712

Defined in: plugins/fs/guest-js/index.ts:777

Options for the functions that read a file, such as readFile and readTextFile.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:779
encoding? string Text encoding to use when reading a text file. Defaults to ‘utf-8’. plugins/fs/guest-js/index.ts:781

Defined in: plugins/fs/guest-js/index.ts:948

Options for the remove function, which deletes a file or a directory.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:952
recursive? boolean Defaults to false. If set to true, path will be removed even if it’s a non-empty directory. plugins/fs/guest-js/index.ts:950

Defined in: plugins/fs/guest-js/index.ts:988

Options for the rename function, defining the base directory of each path.

2.0.0

Property Type Description Defined in
newPathBaseDir? BaseDirectory Base directory for newPath. plugins/fs/guest-js/index.ts:992
oldPathBaseDir? BaseDirectory Base directory for oldPath. plugins/fs/guest-js/index.ts:990

Defined in: plugins/fs/guest-js/index.ts:1521

Options for the size function.

2.6.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path. plugins/fs/guest-js/index.ts:1523

Defined in: plugins/fs/guest-js/index.ts:1037

Options for the stat and lstat functions, which read the metadata of a path.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path. plugins/fs/guest-js/index.ts:1039

Defined in: plugins/fs/guest-js/index.ts:1104

Options for the truncate function, which truncates or extends a file.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path. plugins/fs/guest-js/index.ts:1106

Defined in: plugins/fs/guest-js/index.ts:1324

A file system change reported to the callback of watch or watchImmediate.

2.0.0

Property Type Description Defined in
attrs unknown Additional attributes reported by the underlying file system watcher. plugins/fs/guest-js/index.ts:1330
paths string[] The paths affected by the change. plugins/fs/guest-js/index.ts:1328
type WatchEventKind The kind of change that was detected. plugins/fs/guest-js/index.ts:1326

Defined in: plugins/fs/guest-js/index.ts:1299

Options for the watchImmediate function, which reports file system changes as they happen.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:1303
recursive? boolean Watch a directory recursively plugins/fs/guest-js/index.ts:1301

Defined in: plugins/fs/guest-js/index.ts:1153

Options for the writeFile and writeTextFile functions, defining how the file is opened before writing to it.

2.0.0

Property Type Description Defined in
append? boolean Defaults to false. If set to true, will append to a file instead of overwriting previous contents. plugins/fs/guest-js/index.ts:1155
baseDir? BaseDirectory Base directory for path plugins/fs/guest-js/index.ts:1163
create? boolean Sets the option to allow creating a new file, if one doesn’t already exist at the specified path (defaults to true). plugins/fs/guest-js/index.ts:1157
createNew? boolean Sets the option to create a new file, failing if it already exists. plugins/fs/guest-js/index.ts:1159
mode? number File permissions. Ignored on Windows. plugins/fs/guest-js/index.ts:1161

type UnwatchFn = () => void;

Defined in: plugins/fs/guest-js/index.ts:1407

Stops watching the paths it was created for. Returned by watch and watchImmediate.

void

2.0.0


type WatchEventKind =
| "any"
| {
access: WatchEventKindAccess;
}
| {
create: WatchEventKindCreate;
}
| {
modify: WatchEventKindModify;
}
| {
remove: WatchEventKindRemove;
}
| "other";

Defined in: plugins/fs/guest-js/index.ts:1338

The kind of file system change described by a WatchEvent.

2.0.0


type WatchEventKindAccess =
| {
kind: "any";
}
| {
kind: "close";
mode: "any" | "execute" | "read" | "write" | "other";
}
| {
kind: "open";
mode: "any" | "execute" | "read" | "write" | "other";
}
| {
kind: "other";
};

Defined in: plugins/fs/guest-js/index.ts:1351

Describes how a file or directory was accessed.

2.0.0


type WatchEventKindCreate =
| {
kind: "any";
}
| {
kind: "file";
}
| {
kind: "folder";
}
| {
kind: "other";
};

Defined in: plugins/fs/guest-js/index.ts:1362

Describes which kind of entry was created.

2.0.0


type WatchEventKindModify =
| {
kind: "any";
}
| {
kind: "data";
mode: "any" | "size" | "content" | "other";
}
| {
kind: "metadata";
mode: | "any"
| "access-time"
| "write-time"
| "permissions"
| "ownership"
| "extended"
| "other";
}
| {
kind: "rename";
mode: "any" | "to" | "from" | "both" | "other";
}
| {
kind: "other";
};

Defined in: plugins/fs/guest-js/index.ts:1373

Describes what was modified on a file or directory.

2.0.0


type WatchEventKindRemove =
| {
kind: "any";
}
| {
kind: "file";
}
| {
kind: "folder";
}
| {
kind: "other";
};

Defined in: plugins/fs/guest-js/index.ts:1395

Describes which kind of entry was removed.

2.0.0


© 2026 Tauri Contributors. CC-BY / MIT