Search code examples
node.jstauri

Is there a way to include NPM packages in a Tauri desktop application?


So a bit more background: I have built a Tauri application that just uses Vanilla Javascript/HTML/CSS and the usual Rust backend. I want to include an NPM package to make my life easier but I cannot figure out how to get it to integrate it into my application. I read something about a 'sidecar' but honestly the article from the Tauri website was hard to follow. Was wondering if any of the good folks of SO could point me in the right direction of an article or video or even code example of how this could work.

Thanks!

So far I have tried, npm i serialport (thats the npm package im trying to use btw) and the importing the package using named imports and default imports. Im assuming its because Tauri does not come baked in with a Node.js environment like a web framework per-se. I migrated this app from Electron to Tauri because of the performance and package size differences.


Solution

  • Only browser NPM packages can be used with Tauri. There is no Node runtime with a Tauri application to run the dependency on, so anything like serial port interaction would need to be a Rust library.

    The only NPM packages you can use are for the UI side of the Tauri application, and that also means they run inside the browser sandbox, and therefore they don't have native access to system resources beyond the web API.

    The correct way forward here is to use a Rust library for this purpose and then register Tauri handlers that invoke that code. Those can then be called from the front end using Events. Maybe serialport-rs fits your purpose?

    Theoretically, you could have a separate Node app that the Rust code calls, but this isn't in the spirit of Tauri and not embracing it as it should be.