Integrate into Existing Project
If you already have an existing web project, this guide will walk you through integrating Tauri into your project, whether it is Node.js-based (like Svelte, React, Vue, or Angular) or Rust-based (like Yew or Dominator).
Before we continue, make sure you have completed the prerequisites to have a working development environment.
Although Tauri is compatible with nearly any frontend framework, we'll use a React project throughout this guide created using create-react-app. We'll be assuming you're starting with a project structure similar to this:
.
βββ package.json
βββ public
β β°ββ index.html
β°ββ src
βββ App.css
βββ App.jsx
βββ index.css
β°ββ index.js
Create the Rust Projectβ
At the heart of every Tauri app is a Rust binary that manages windows, the webview, and calls to the operating system through a Rust crate called tauri
. This project is managed by Cargo, the official package manager and general-purpose build tool for Rust.
Our Tauri CLI uses Cargo under the hood so you rarely need to interact with it directly. Cargo has many more useful features that are not exposed through our CLI, such as testing, linting, and formatting, so please refer to their official docs for more.
If you haven't installed the Tauri CLI yet you can do so with one of the below commands. Aren't sure which to use? Check out the FAQ entry.
- npm
- Yarn
- pnpm
- Bun
- Cargo
npm install --save-dev @tauri-apps/cli
"scripts": {
"tauri": "tauri"
}
yarn add -D @tauri-apps/cli
pnpm add -D @tauri-apps/cli
bun add -D @tauri-apps/cli
cargo install tauri-cli
To scaffold a minimal Rust project that is pre-configured to use Tauri, open a terminal and run the following command:
- npm
- Yarn
- pnpm
- Bun
- Cargo
npm run tauri init
yarn tauri init
pnpm tauri init
bunx tauri init
cargo tauri init
It will walk you through a series of questions:
- What is your app name?
This will be the name of your final bundle and what the OS will call your app. You can use any name you want here. - What should the window title be?
This will be the title of the default main window. You can use any title you want here. - Where are your web assets (HTML/CSS/JS) located relative to the
<current dir>/src-tauri/tauri.conf.json
file that will be created?
This is the path that Tauri will load your frontend assets from when building for production.For the project example in this guide, this is../build
. Note that it may be something different like../dist
if you're using a different framework. - What is the URL of your dev server?
This can be either a URL or a file path that Tauri will load during development.For the project example in this guide, this ishttp://localhost:3000
. Note that it may be something different (or even a directory) if you're using a different framework. - What is your frontend dev command?
This is the command used to start your frontend dev server.For the project example in this guide, this isnpm run start
(make sure to adapt this to use the package manager of your choice). - What is your frontend build command?
This is the command to build your frontend files.For the project example in this guide, this isnpm run build
(make sure to adapt this to use the package manager of your choice).
If you're familiar with Rust, you will notice that tauri init
looks and works a lot like cargo init
. You can just use cargo init
and add the necessary Tauri dependencies if you prefer a fully manual setup.
The tauri init
command generates a folder called src-tauri
. It's a convention for Tauri apps to place all core-related files into this folder. Let's quickly run through the contents of this folder:
Cargo.toml
Cargo's manifest file. You can declare Rust crates your app depends on, metadata about your app, and much more. For the full reference see Cargo's Manifest Format.tauri.conf.json
This file lets you configure and customize aspects of your Tauri application from the name of your app to the list of allowed APIs. See Tauri's API Configuration for the full list of supported options and in-depth explanations for each.src/main.rs
This is the entry point to your Rust program and the place where we bootstrap into Tauri. You will find two sections in it:src/main.rs#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("error while running tauri application");
}The line beginning with the
cfg! macro
serves just one purpose: it disables the command prompt window that would normally pop up on Windows if you run a bundled app. If you're on Windows, try to comment it out and see what happens.The
main
function is the entry point and the first function that gets invoked when your program runs.icons
Chances are you want a snazzy icon for your app! To get you going quickly, we included a set of default icons. You should switch these out before publishing your application. Learn more about the various icon formats in Tauri's icons feature guide.
And that's it, you have now added Tauri to your existing project and you should see a src-tauri
directory that looks something like this:
βββ package.json
βββ public
β β°ββ index.html
βββ src
β βββ App.css
β βββ App.jsx
β βββ index.css
β β°ββ index.js
β°ββ src-tauri
βββ Cargo.toml
βββ build.rs
βββ icons
βββ src
β°ββ tauri.conf.json
Invoke Commandsβ
Tauri lets you enhance your frontend with native capabilities. We call these Commands, essentially Rust functions that you can call from your frontend JavaScript. This enables you to handle heavy processing or calls to the OS in much more performant Rust code.
Let's make a simple example:
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
A Command is just like any regular Rust function, with the addition of the #[tauri::command]
attribute macro that allows your function to communicate with the JavaScript context.
Lastly, we also need to tell Tauri about our newly created command so that it can route calls accordingly. This is done with the combination of the .invoke_handler()
function and the generate_handler![]
macro you can see below:
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Now you're ready to call your Command from the frontend!
There are two different ways you can invoke commands from your frontend project:
- Using the
@tauri-apps/api
JavaScript library (preferred) - Using
withGlobalTauri
to use a pre-bundled version of the Tauri API
We'll go through both below.
Using JavaScript Libraryβ
To call our newly created command we will use the @tauri-apps/api
JavaScript library. It provides access to core functionality such as windows, the filesystem, and more through convenient JavaScript abstractions. You can install it using your favorite JavaScript package manager:
- npm
- Yarn
- pnpm
- Bun
npm install @tauri-apps/api
yarn add @tauri-apps/api
pnpm add @tauri-apps/api
bun add @tauri-apps/api
You can now import the invoke
function from the library and use it to call our command:
import logo from './logo.svg';
import './App.css';
import { invoke } from '@tauri-apps/api'
function App() {
// now we can call our Command!
// Right-click the application background and open the developer tools.
// You will see "Hello, World!" printed in the console!
invoke('greet', { name: 'World' })
// `invoke` returns a Promise
.then((response) => console.log(response))
return (
// -- snip --
)
}
Using withGlobalTauri
β
To interact with Tauri from your frontend without using the @tauri-apps/api
JavaScript package you will need to enable withGlobalTauri
in your tauri.conf.json
file:
{
"build": {
"beforeBuildCommand": "npm run build",
"beforeDevCommand": "npm run dev",
"devPath": "http://localhost:3000",
"distDir": "../build",
"withGlobalTauri": true
},
This will inject a pre-bundled version of the API functions into your frontend.
You can now modify the App.jsx
file to call your command:
import logo from './logo.svg';
import './App.css';
// access the pre-bundled global API functions
const { invoke } = window.__TAURI__.tauri
function App() {
// now we can call our Command!
// Right-click the application background and open the developer tools.
// You will see "Hello, World!" printed in the console!
invoke('greet', { name: 'World' })
// `invoke` returns a Promise
.then((response) => console.log(response))
return (
// -- snip --
)
}
Running Your Appβ
You can now run the following command in your terminal to start a development build of your app:
- npm
- Yarn
- pnpm
- bun
- Cargo
npm run tauri dev
yarn tauri dev
pnpm tauri dev
bunx tauri dev
cargo tauri dev
If you want to know more about the communication between Rust and JavaScript, please read the Tauri Inter-Process Communication guide.