<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zaid Sunasra</title>
    <description>The latest articles on DEV Community by Zaid Sunasra (@zaid_sunasra).</description>
    <link>https://dev.to/zaid_sunasra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1876914%2Ffc92b5c4-7eff-497a-86bb-21c1db99eddd.png</url>
      <title>DEV Community: Zaid Sunasra</title>
      <link>https://dev.to/zaid_sunasra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zaid_sunasra"/>
    <language>en</language>
    <item>
      <title>Adding Node.js server to Tauri App as a sidecar</title>
      <dc:creator>Zaid Sunasra</dc:creator>
      <pubDate>Sat, 11 Jan 2025 19:35:48 +0000</pubDate>
      <link>https://dev.to/zaid_sunasra/adding-nodejs-server-to-tauri-app-as-a-sidecar-509j</link>
      <guid>https://dev.to/zaid_sunasra/adding-nodejs-server-to-tauri-app-as-a-sidecar-509j</guid>
      <description>&lt;h1&gt;
  
  
  The Journey of Integrating Node.js as a Sidecar in Tauri
&lt;/h1&gt;

&lt;p&gt;In this article, I share my experience tackling a challenge many Tauri developers might encounter—connecting a Node.js server as a sidecar in a Tauri app. Initially, the process seemed straightforward, but I ran into multiple roadblocks. The lack of comprehensive resources or clear solutions online made it an even steeper climb.&lt;/p&gt;

&lt;p&gt;After trying various approaches, I turned to the Tauri GitHub discussions for help. It was there that a maintainer, Fabian Lars, stepped in and provided guidance that clarified the situation and helped resolve the issue. This article aims to highlight the problem I faced, share insights from the discussion, and document the lessons I learned along the way.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create Tauri App
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Note: Make sure you have the prerequisites for using Tauri installed.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://v2.tauri.app/start/prerequisites/" rel="noopener noreferrer"&gt;Check pre-requisites&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create tauri-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or visit &lt;strong&gt;&lt;a href="https://v2.tauri.app/start/create-project/" rel="noopener noreferrer"&gt;docs&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a Nodejs package
&lt;/h4&gt;

&lt;p&gt;The following commands will convert your node.js file into an executable(.exe). Replace &lt;code&gt;file_name&lt;/code&gt; with the name of your node.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g pkg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Packaging &lt;code&gt;file_name.js&lt;/code&gt; into an &lt;code&gt;.exe&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg file_name.js --targets node18-win-x64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above will crate an .exe file for windows. &lt;br&gt;
&lt;strong&gt;For Linux&lt;/strong&gt;: &lt;code&gt;node18-linux-x64&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;For MacOS&lt;/strong&gt;: &lt;code&gt;node18-macos-x64&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create 3 .exe at a time use following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg index.js --targets node18-win-x64,node18-linux-x64,node18-macos-x64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an &lt;code&gt;file_name.exe&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note- But we need to rename the file as it is required for Tauri to identify it as a sidecar. Name format required for .exe file is:&lt;/em&gt; &lt;code&gt;any_name-${targetTriple}${extension}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;You can find your current platform’s &lt;code&gt;-$TARGET_TRIPLE&lt;/code&gt; suffix by looking at the &lt;code&gt;host:&lt;/code&gt; property by running the following code in terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rustc -Vv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2putotq8l8wtjy8157da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2putotq8l8wtjy8157da.png" alt="Image of host value" width="386" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the host property from above command and rename &lt;code&gt;file_name.js&lt;/code&gt;&lt;br&gt;
to &lt;code&gt;file_name.js-host_property_value.exe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;file_name-x86_64-pc-windows-msvc.exe&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Configuring our main file
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Go to &lt;code&gt;src-tauri&lt;/code&gt;. Create a folder name &lt;code&gt;binaries&lt;/code&gt;. In the folder place the above &lt;code&gt;.exe&lt;/code&gt; created.&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Go to &lt;code&gt;src-tauri/tauri.conf.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "bundle": {
    "externalBin": [
      "binaries/file_name"
    ]
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: Just add the file name.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdyjm6dpouiannrbpj6x6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdyjm6dpouiannrbpj6x6.png" alt="Instruction " width="800" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run tauri add shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @tauri-apps/plugin-shell 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Copy the following code to &lt;code&gt;src-tauri/capabilities/default.json&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Note: You will see warning in this file saying folder not present. Ignore the warnings as the folder will be generated once you follow all the steps.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "permissions": [
    "core:default",
    "opener:default",
    "shell:default",
    {
      "identifier": "shell:allow-spawn",
      "allow": [
        {
          "name": "binaries/file_name",
          "sidecar": true
        }
      ]
    },
    "shell:allow-open"
  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; Add the following code in &lt;code&gt;src/main.tsx&lt;/code&gt; or &lt;code&gt;src/main.jsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Command } from '@tauri-apps/plugin-shell';


const command = Command.sidecar('binaries/file_name', ["args", "if", "any"]);
await command.spawn();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6.&lt;/strong&gt; Run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run tauri dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;You have successfully integrated your server into the Tauri app. To verify the setup, open the app, and it should prompt for permission to run Node.js. Alternatively, you can check the server's status by visiting &lt;code&gt;localhost&lt;/code&gt; and the port it is running on in your browser.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>node</category>
      <category>softwaredevelopment</category>
      <category>tauri</category>
    </item>
  </channel>
</rss>
