Node.js as a sidecar
Esta página aún no está disponible en tu idioma.
In this guide we are going to package a Node.js application to a self contained binary to be used as a sidecar in a Tauri application without requiring the end user to have a Node.js installation. This example tutorial is applicable for desktop operating systems only.
We recommend reading the general sidecar guide first for a deeper understanding of how Tauri sidecars work.
In this example we will create a Node.js application that reads input from the command line process.argv
and writes output to stdout using console.log.
You can leverage alternative inter-process communication systems such as a localhost server, stdin/stdout or local sockets.
Note that each has their own advantages, drawbacks and security concerns.
Prerequisites
An existing Tauri application set up with the shell plugin, that compiles and runs for you locally.
Guide
-
Let’s create a new Node.js project to contain our sidecar implementation. Create a new directory in your Tauri application root folder (in this example we will call it
sidecar-app
) and run theinit
command of your preferred Node.js package manager inside the directory:We will compile our Node.js application to a self container binary using pkg. Let’s install it as a development dependency:
-
Write Sidecar Logic
Now we can start writing JavaScript code that will be executed by our Tauri application.
In this example we will process a command from the command line argmuents and write output to stdout, which means our process will be short lived and only handle a single command at a time. If your application must be long lived, consider using alternative inter-process communication systems.
Let’s create a
index.js
file in oursidecar-app
directory and write a basic Node.js app: -
To package our Node.js application to a self contained binary, we can run the following
pkg
command:This will create the
sidecar-app/app
binary on Linux and macOS, and asidecar-app/app.exe
executable on Windows. To rename this file to the expected Tauri sidecar filename, we can use the following Node.js script: -
Configure the Sidecar in the Tauri Application
Now that we have our Node.js application ready, we can connect it to our Tauri application by configuring the
bundle > externalBin
array:The Tauri CLI will handle the bundling of the sidecar binary as long as it exists as
src-tauri/binaries/app-<target-triple>
. -
We can run the sidecar binary either from Rust code or directly from JavaScript.
Let’s execute the
ping
command in the Node.js sidecar directly:Let’s pipe a
ping
Tauri command to the Node.js sidecar:
© 2024 Tauri Contributors. CC-BY / MIT