Skip to content

Cannot import @tauri-apps/api/tauri #1512

@Pierstoval

Description

@Pierstoval

Describe the bug
Cannot import @tauri-apps/api/tauri from a <script lang="ts"> tag when frontend is build with Svelte kit and latest Tauri version.

The project was created today, with a totally fresh Tauri install (using the new beta version), and the frontend is a plain Svelte Kit application with one single change: instead of using the node bundle adapter, I'm using the static one, in order to always build an HTML/CSS app instead of a Node.js one, because Tauri needs a static app.

What I intend to do is simply use the invoke function from the Tauri API in order to communicate with the Rust backend (and I just want to return a string).

This is sort of a "test app", so nothing complex at all in here, hence why I create an issue, because I think a freshly bootstrapped app like that shouldn't have bugs, and I don't know if the issue comes from me or Tauri or Svelte.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the rewrite branch of this repo: https://github.com/Orbitale/Compotes/tree/rewrite (NOTE in 2025: it was years ago, this is no longer relevant, but I just keep the link for history reasons)
  2. Install everything with yarn install, then run yarn dev to start the frontend server
  3. Start the Tauri app with yarn tauri dev in another terminal instance
  4. The app should automatically rebuild, and you should see this frontend error:
(click to display stack trace 👇)
Cannot find module '@tauri-apps/api/tauri' from '/var/www/Compotes2/src/routes'

Error: Cannot find module '@tauri-apps/api/tauri' from '/var/www/Compotes2/src/routes'
    at Function.resolveSync [as sync] (/var/www/Compotes2/node_modules/resolve/lib/sync.js:102:15)
    at resolveFrom$2 (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:3884:29)
    at resolve (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69065:22)
    at nodeRequire (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69044:25)
    at ssrImport (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69002:20)
    at eval (/var/www/Compotes2/src/routes/index.svelte:7:31)
    at instantiateModule (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)

The problem occurs because of the src/routes/index.svelte:3 statement which imports the Tauri API.

Of course, when I check the node_modules directory, the package is here:

$ ls node_modules/@tauri-apps/api
CHANGELOG.md  dist  LICENSE_APACHE-2.0  LICENSE_MIT  package.json

And I can definitely see the dist/api.js file.

Expected behavior
I expect to be able to load any of the Tauri JS API exports, such as invoke (which is tested here).

Platform and Versions (please complete the following information):

OS: Ubuntu 20.04 in WSL 2, and display is sent to the Windows host using an instance of VcXsrv.
Node: 15.14.0 (installed via NVM)
NPM: 7.7.6 (though not used)
Yarn: 1.22.10 (installed via npm)
Rustc: 1.51.0

Additional notes

The current import value "../../node_modules/@tauri-apps/api/dist/tauri.js" is partially working: the import occurs properly, but executing invoke(...) with arguments doesn't work.
The frontend server crashes with this error:

(click to display error 👇)
ReferenceError: window is not defined
    at eval (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:2093)
    at a (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:2227)
    at eval (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:2527)
    at new Promise (<anonymous>)
    at Module.eval (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:2494)
    at eval (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:1903)
    at Object.eval [as next] (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:2008)
    at eval (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:945)
    at new Promise (<anonymous>)
    at r (/node_modules/@tauri-apps/api/dist/tauri-ac52b223.js:17:690)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I think it might be caused by Svelte, but this goes way above my current knowledge of both Tauri and Svelte (I'm a Svelte newcomer, I only built one purely static portfolio with it, nothing more yet).

Another additional note

I also tested on native Windows with the same version of all system dependencies, I even upgraded Build Tools from 16.8.5 to 16.9.4, but I couldn't run yarn tauri dev, which throws this error:

(click to display error 👇)
$ yarn tauri dev
yarn run v1.22.5
$ C:\dev\Compotes2\node_modules\.bin\tauri dev
 app:spawn Running "C:\dev\Compotes2\node_modules\@tauri-apps\cli\bin\tauri-cli.exe tauri dev" +0ms

   Compiling app v0.1.0 (C:\dev\Compotes2\src-tauri)
    Finished dev [unoptimized + debuginfo] target(s) in 3.92s
     Running `target\debug\app.exe`
thread 'main' panicked at 'error while running tauri application: CreateWebview', src\main.rs:12:6
stack backtrace:
   0:     0x7ff7bf63273e - std::backtrace_rs::backtrace::dbghelp::trace
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:98
   1:     0x7ff7bf63273e - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff7bf63273e - std::sys_common::backtrace::_print_fmt
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\sys_common\backtrace.rs:67
   3:     0x7ff7bf63273e - std::sys_common::backtrace::_print::{{impl}}::fmt
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\sys_common\backtrace.rs:46
   4:     0x7ff7bf6518eb - core::fmt::write
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\core\src\fmt\mod.rs:1096
   5:     0x7ff7bf62a9c8 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\io\mod.rs:1568
   6:     0x7ff7bf63556d - std::sys_common::backtrace::_print
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\sys_common\backtrace.rs:49
   7:     0x7ff7bf63556d - std::sys_common::backtrace::print
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\sys_common\backtrace.rs:36
   8:     0x7ff7bf63556d - std::panicking::default_hook::{{closure}}
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:208
   9:     0x7ff7bf635039 - std::panicking::default_hook
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:225
  10:     0x7ff7bf635e20 - std::panicking::rust_panic_with_hook
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:591
  11:     0x7ff7bf635951 - std::panicking::begin_panic_handler::{{closure}}
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:497
  12:     0x7ff7bf6330cf - std::sys_common::backtrace::__rust_end_short_backtrace<closure-0,!>
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\sys_common\backtrace.rs:141
  13:     0x7ff7bf6358a9 - std::panicking::begin_panic_handler
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:493
  14:     0x7ff7bf64f720 - core::panicking::panic_fmt
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\core\src\panicking.rs:92
  15:     0x7ff7bf64f543 - core::option::expect_none_failed
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\core\src\option.rs:1300
  16:     0x7ff7be9ca8a5 - core::result::Result<tuple<>, tauri::error::Error>::expect<tuple<>,tauri::error::Error>
                               at C:\Users\Pierstoval\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:997
  17:     0x7ff7beb376c1 - app::main
                               at C:\dev\Compotes2\src-tauri\src\main.rs:7
  18:     0x7ff7be8dfbbb - core::ops::function::FnOnce::call_once<fn(),tuple<>>
                               at C:\Users\Pierstoval\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
  19:     0x7ff7beaa990b - std::sys_common::backtrace::__rust_begin_short_backtrace<fn(),tuple<>>
                               at C:\Users\Pierstoval\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys_common\backtrace.rs:125
  20:     0x7ff7be8bc4f1 - std::rt::lang_start::{{closure}}<tuple<>>
                               at C:\Users\Pierstoval\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:66
  21:     0x7ff7bf6360f7 - core::ops::function::impls::{{impl}}::call_once
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\library\core\src\ops\function.rs:280
  22:     0x7ff7bf6360f7 - std::panicking::try::do_call
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:379
  23:     0x7ff7bf6360f7 - std::panicking::try
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panicking.rs:343
  24:     0x7ff7bf6360f7 - std::panic::catch_unwind
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\panic.rs:431
  25:     0x7ff7bf6360f7 - std::rt::lang_start_internal
                               at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0\/library\std\src\rt.rs:51
  26:     0x7ff7be8bc4c3 - std::rt::lang_start<tuple<>>
                               at C:\Users\Pierstoval\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:65
  27:     0x7ff7beb37940 - main
  28:     0x7ff7bf6d9610 - invoke_main
                               at D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  29:     0x7ff7bf6d9610 - __scrt_common_main_seh
                               at D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  30:     0x7fffc9917034 - BaseThreadInitThunk
  31:     0x7fffcb222651 - RtlUserThreadStart
error: process didn't exit successfully: `target\debug\app.exe` (exit code: 101)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions