Aller au contenu

@tauri-apps/plugin-fs

Ce contenu n’est pas encore disponible dans votre langue.

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.

Source: undefined

2.0.0

AppCache: 16;

Source: undefined

appCacheDir for more information.

AppConfig: 13;

Source: undefined

appConfigDir for more information.

AppData: 14;

Source: undefined

appDataDir for more information.

AppLocalData: 15;

Source: undefined

appLocalDataDir for more information.

AppLog: 17;

Source: undefined

appLogDir for more information.

Audio: 1;

Source: undefined

audioDir for more information.

Cache: 2;

Source: undefined

cacheDir for more information.

Config: 3;

Source: undefined

configDir for more information.

Data: 4;

Source: undefined

dataDir for more information.

Desktop: 18;

Source: undefined

desktopDir for more information.

Document: 6;

Source: undefined

documentDir for more information.

Download: 7;

Source: undefined

downloadDir for more information.

Executable: 19;

Source: undefined

executableDir for more information.

Font: 20;

Source: undefined

fontDir for more information.

Home: 21;

Source: undefined

homeDir for more information.

LocalData: 5;

Source: undefined

localDataDir for more information.

Picture: 8;

Source: undefined

pictureDir for more information.

Public: 9;

Source: undefined

publicDir for more information.

Resource: 11;

Source: undefined

resourceDir for more information.

Runtime: 22;

Source: undefined

runtimeDir for more information.

Temp: 12;

Source: undefined

tempDir for more information.

Template: 23;

Source: undefined

templateDir for more information.

Video: 10;

Source: undefined

videoDir for more information.


Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L94

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

Current: 1;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L98

The offset is relative to the current cursor position.

End: 2;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L100

The offset is relative to the end of the file.

Start: 0;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L96

The offset is relative to the start of the file.

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L301

The Tauri abstraction for reading and writing files.

2.0.0

  • Resource

new FileHandle(rid): FileHandle;

Source: undefined

Parameter Type
rid number

FileHandle

Resource.constructor

get rid(): number;

Source: undefined

number

Resource.rid

close(): Promise<void>;

Source: undefined

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>

Resource.close

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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L335

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.

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.

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();

2.0.0

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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L393

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.

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.

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();

2.0.0

stat(): Promise<FileInfo>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L416

Returns a FileInfo for this file.

Promise<FileInfo>

A promise resolving to the metadata of 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();

2.0.0

truncate(len?): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L449

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

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>

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();

2.0.0

write(data): Promise<number>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L478

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.

Parameter Type Description
data Uint8Array The bytes written to the file.

Promise<number>

A promise resolving to the number of bytes written.

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();

2.0.0

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L624

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L626
toPathBaseDir? BaseDirectory Base directory for toPath. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L628

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L491

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L493

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1309

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1301
delayMs? number The debounce delay in milliseconds. Changes that happen within this window are grouped and reported together. Defaults to 2000. - Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1314
recursive? boolean Watch a directory recursively WatchOptions.recursive Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1299

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L722

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L726
isFile boolean Specifies whether this entry is a file or not. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L728
isSymlink boolean Specifies whether this entry is a symlink or not. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L730
name string The name of the entry (file name with extension or directory name). Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L724

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1259

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1261

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L108

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L139
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L145
blksize number | null Blocksize for filesystem I/O. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L222
blocks number | null Number of blocks allocated to the file, in 512-byte units. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L230
dev number | null ID of the device containing the file. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L165
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L157
gid number | null Group ID of the owner of this file. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L206
ino number | null Inode number. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L173
isDirectory boolean True if this is info for a regular directory. Mutually exclusive to FileInfo.isFile and FileInfo.isSymlink. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L118
isFile boolean True if this is info for a regular file. Mutually exclusive to FileInfo.isDirectory and FileInfo.isSymlink. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L113
isSymlink boolean True if this is info for a symlink. Mutually exclusive to FileInfo.isFile and FileInfo.isDirectory. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L123
mode number | null The underlying raw st_mode bits that contain the standard Unix permissions for this file/directory. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L182
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L133
nlink number | null Number of hard links pointing to this file. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L190
rdev number | null Device ID of this file. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L214
readonly boolean Whether this is a readonly (unwritable) file. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L147
size number The size of the file, in bytes. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L127
uid number | null User ID of the owner of this file. Platform-specific - Windows: Unsupported. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L198

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L668

Options for the mkdir function, which creates a directory.

2.0.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L676
mode? number Permissions to use when creating the directory (defaults to 0o777, before the process’s umask). Ignored on Windows. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L670
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). Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L674

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L534

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 }. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L553
baseDir? BaseDirectory Base directory for path Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L581
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L566
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L573
mode? number Permissions to use if creating the file (defaults to 0o666, before the process’s umask). Ignored on Windows. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L579
read? boolean Sets the option for read access. This option, when true, means that the file should be read-able if opened. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L539
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L560
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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L546

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L710

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L712

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L777

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L779
encoding? string Text encoding to use when reading a text file. Defaults to ‘utf-8’. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L781

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L948

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L952
recursive? boolean Defaults to false. If set to true, path will be removed even if it’s a non-empty directory. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L950

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L988

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L992
oldPathBaseDir? BaseDirectory Base directory for oldPath. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L990

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1519

Options for the size function.

2.6.0

Property Type Description Defined in
baseDir? BaseDirectory Base directory for path. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1521

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1037

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1039

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1104

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1106

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1322

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1328
paths string[] The paths affected by the change. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1326
type WatchEventKind The kind of change that was detected. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1324

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1297

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 Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1301
recursive? boolean Watch a directory recursively Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1299

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1153

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. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1155
baseDir? BaseDirectory Base directory for path Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1163
create? boolean Sets the option to allow creating a new file, if one doesn’t already exist at the specified path (defaults to true). Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1157
createNew? boolean Sets the option to create a new file, failing if it already exists. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1159
mode? number File permissions. Ignored on Windows. Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1161

type UnwatchFn = () => void;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1405

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";

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1336

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";
};

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1349

Describes how a file or directory was accessed.

2.0.0


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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1360

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";
};

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1371

Describes what was modified on a file or directory.

2.0.0


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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1393

Describes which kind of entry was removed.

2.0.0

function copyFile(
fromPath,
toPath,
options?
): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L644

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

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>

import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });

2.0.0


function create(path, options?): Promise<FileHandle>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L513

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

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.

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();

2.0.0


function exists(path, options?): Promise<boolean>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1278

Check if a path exists.

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.

import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
// Check if the `$APPDATA/avatar.png` file exists
await exists('avatar.png', { baseDir: BaseDirectory.AppData });

2.0.0


function lstat(path, options?): Promise<FileInfo>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1087

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.

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.

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

2.0.0


function mkdir(path, options?): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L691

Creates a new directory with the specified path.

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>

import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
await mkdir('users', { baseDir: BaseDirectory.AppLocalData });

2.0.0


function open(path, options?): Promise<FileHandle>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L603

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.

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.

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();

2.0.0


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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L758

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

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.

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 }));
}
}
}

2.0.0


function readFile(path, options?): Promise<Uint8Array<ArrayBuffer>>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L798

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.

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

Promise<Uint8Array<ArrayBuffer>>

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

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

2.0.0


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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L827

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

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.

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

2.0.0


function readTextFileLines(path, options?): Promise<AsyncIterableIterator<string, any, any>>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L863

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

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, any, any>>

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

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.

2.0.0


function remove(path, options?): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L969

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.

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>

import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';
await remove('users/file.txt', { baseDir: BaseDirectory.AppLocalData });
await remove('users', { baseDir: BaseDirectory.AppLocalData });

2.0.0


function rename(
oldPath,
newPath,
options?
): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1013

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.

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>

import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';
await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });

2.0.0


function size(path, options?): Promise<number>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1542

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.

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.

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

2.1.0


function startAccessingSecurityScopedResource(path): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1583

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.
Parameter Type Description
path string | URL The path or file:// URL of the resource to start accessing.

Promise<void>

import { startAccessingSecurityScopedResource } from '@tauri-apps/plugin-fs';
const filePath = 'file:///path/to/file.txt';
await startAccessingSecurityScopedResource(filePath);
// ... use the resource ...

2.5.0


function stat(path, options?): Promise<FileInfo>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1058

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.

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.

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

2.0.0


function stopAccessingSecurityScopedResource(path): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1619

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.
Parameter Type Description
path string | URL The path or file:// URL of the resource to stop accessing.

Promise<void>

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);

2.5.0


function truncate(
path,
len?,
options?
): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1132

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.

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>

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"

2.0.0


function watch(
paths,
cb,
options?
): Promise<UnwatchFn>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1465

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.

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) => 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.

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();

2.0.0


function watchImmediate(
paths,
cb,
options?
): Promise<UnwatchFn>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1503

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.

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) => 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.

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();

2.0.0


function writeFile(
path,
data,
options?
): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1182

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

Parameter Type Description
path string | URL The path of the file to write to.
data | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> 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>

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 });

2.0.0


function writeTextFile(
path,
data,
options?
): Promise<void>;

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1235

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

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>

import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });

2.0.0


© 2026 Tauri Contributors. CC-BY / MIT