新しく Tauri 2 のプロジェクトを作る方法は 2 つあります。フロントエンドごと雛形を生成する create-tauri-app と、すでにある Web フロントエンドのリポジトリに src-tauri/ を追加する tauri init です。前者は最短で動くものが欲しいとき、後者は既存の Vite / React プロジェクトをデスクトップ化したいときに使います。
1. create-tauri-app で新規作成する
npm create tauri-app@latest
# yarn create tauri-app / pnpm create tauri-app / bun create tauri-app でも可
対話形式の質問と、その答えがどこに反映されるかを整理します。
| 質問 | 反映先 | 補足 |
|---|---|---|
| Project name | ディレクトリ名、package.json と Cargo.toml の name、productName | . でカレントディレクトリに展開 |
| Identifier | tauri.conf.json の identifier | 後述。既定は com.<project>.app |
| Frontend language | TypeScript / JavaScript、Rust (Yew / Leptos / Dioxus)、.NET (Blazor) | 迷ったら TS/JS |
| Package manager | npm / pnpm / yarn / bun | beforeDevCommand に npm run dev などとして書き込まれる |
| UI template | Vanilla / Vue / Svelte / React / Solid / Angular / Preact | Angular 以外は Vite ベース |
| UI flavor | TypeScript / JavaScript | *-ts テンプレートかどうか |
CI やスクリプトから非対話で作るときはフラグで指定します。npm create 経由では -- の後にフラグを置きます。
npm create tauri-app@latest my-app -- \
--template react-ts --manager npm \
--identifier com.example.myapp --yes
--template には vanilla-ts, vue-ts, svelte-ts, react-ts, solid-ts, angular, yew, leptos など (JS 系は -ts なしの JavaScript 版もあり) を指定できます。
生成されるファイル (react-ts の場合)
my-app/
├── index.html / src/ # Vite の通常の React プロジェクト
├── vite.config.ts # port 1420 固定、src-tauri を watch 除外
├── package.json # @tauri-apps/api, @tauri-apps/cli, plugin-opener
└── src-tauri/
├── Cargo.toml # tauri, tauri-build, tauri-plugin-opener, serde
├── build.rs # tauri_build::build() を呼ぶだけ
├── tauri.conf.json # identifier, ウィンドウ, build コマンド, bundle
├── capabilities/default.json# main ウィンドウに core:default と opener:default
├── icons/ # 各 OS 用アイコン (tauri icon で差し替え)
└── src/main.rs, lib.rs # main.rs は my_app_lib::run() を呼ぶだけ。編集は lib.rs
cd my-app
npm install
npm run tauri dev
2. 既存の Vite / React プロジェクトに tauri init で追加する
手順 1: CLI と API パッケージを追加
npm install -D @tauri-apps/cli@latest
npm install @tauri-apps/api@latest
package.json の scripts に "tauri": "tauri" を追加しておくと、以後 npm run tauri <subcommand> で CLI を呼べます。
手順 2: tauri init を実行
npx tauri init
| 質問 | Vite (既定設定) の答え | 反映先 |
|---|---|---|
| What is your app name? | my-app | productName、Cargo.toml の name |
| What should the window title be? | my-app | app.windows[0].title |
Where are your web assets located, relative to src-tauri/tauri.conf.json? | ../dist | build.frontendDist |
| What is the url of your dev server? | http://localhost:5173 | build.devUrl |
| What is your frontend dev command? | npm run dev | build.beforeDevCommand |
| What is your frontend build command? | npm run build | build.beforeBuildCommand |
同じ内容は --frontend-dist や --dev-url などのフラグでも渡せ、--ci で質問を省略できます。
手順 3: vite.config.ts を Tauri 向けに調整
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
clearScreen: false, // Rust のエラーが Vite の画面クリアで消えないように
server: {
port: 5173,
strictPort: true, // ポートが埋まっていても別ポートに逃げない
watch: { ignored: ['**/src-tauri/**'] },
},
envPrefix: ['VITE_', 'TAURI_ENV_*'],
});
strictPort: true が重要です。Vite が 5174 に逃げると devUrl と食い違い、ウィンドウが真っ白になります。
3. identifier の決め方
identifier は逆ドメイン形式 (com.example.myapp) の文字列で、macOS のバンドル ID、Windows のインストーラーやレジストリ、各 OS のアプリデータ保存先 (~/Library/Application Support/<identifier> や %APPDATA%\<identifier>) に使われます。
- 使える文字は英数字・ハイフン・ピリオドのみ。既定値
com.tauri.devのままだとtauri buildが拒否する。 - 自分が管理するドメインを逆順にするのが慣例。持っていなければ
io.github.<ユーザー名>.<アプリ名>が無難。 - リリース後に変えない。別アプリ扱いになり、保存済み設定の場所が変わる。
- Android / iOS 展開の予定があるならハイフンは避ける (Android のパッケージ名規則)。
動作確認
npm run tauri dev で Vite が起動し、続いて Rust のコンパイルが走ってウィンドウが開けば成功です。tauri init で後付けした場合、capabilities/default.json は core:default のみで生成されるため、@tauri-apps/api の invoke は使えますがプラグイン系 API は個別に権限追加が必要です。
よくあるエラーと対処法
npm create tauri-app で --template が無視される
npm では npm create <pkg> -- <args> の -- が必須です。pnpm / yarn では -- を付けません。
tauri init 後にウィンドウが真っ白
devUrl と Vite の実際のポートが違うのが典型です。vite.config.ts に strictPort: true を付け、build.devUrl と揃えます。Next.js などポート 3000 のフレームワークも同様です。
You must change the bundle identifier in tauri.conf.json > identifier という趣旨のエラー
identifier が既定値の com.tauri.dev のままです。上記の決め方に従って書き換えます。
注意点
- Git で除外するのは
src-tauri/target/とsrc-tauri/gen/schemas/です(gen/全体ではありません)。create-tauri-appはsrc-tauri/.gitignoreにこの 2 つを書きますが、tauri initは既存の.gitignoreを変更しないので自分で足します。 frontendDistはtauri.conf.jsonからの相対パスです。プロジェクト直下のdistは../distになります。
