Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Pierstoval opened this issue Apr 15, 2021 · 13 comments
Closed

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

Pierstoval opened this issue Apr 15, 2021 · 13 comments

Comments

@Pierstoval
Copy link
Contributor

Pierstoval commented Apr 15, 2021

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
  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)
@nganhkhoa
Copy link

nganhkhoa commented Apr 16, 2021

It's because tauri was exported differently, possible a bug, try this

import tauriapi from '@tauri-apps/api';
const { invoke } = tauriapi.tauri;

I was confused also.

@Pierstoval
Copy link
Contributor Author

Pierstoval commented Apr 16, 2021

@nganhkhoa Thanks for helping!

I tried your suggestion, but I get another error:

14:32:50 [vite] Error when evaluating SSR module /var/www/Compotes2/src/routes/index.svelte:
/var/www/Compotes2/node_modules/@tauri-apps/api/dist/index.js:1
... contents of the "index.js:1" file ...
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1022:15)
    at Module._compile (node:internal/modules/cjs/loader:1056:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at nodeRequire (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69044:17)
    at ssrImport (/var/www/Compotes2/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69002:20)

Seems like it doesn't import other libs properly 😕

Could it be possible that the @tauri-apps/api package's building process have flaws somewhere?

@mq1
Copy link
Contributor

mq1 commented Apr 16, 2021

Same error with Vite + Vue + vite-ssg

@nganhkhoa
Copy link

I used React (Javascript and Typescript) and that works, not sure about Svelte though. I guess they need to revise this export model.

@nothingismagick
Copy link
Sponsor Member

Yeah, we have identified the issue and will be resolving this and a number of other minor issues over the weekend. Hang in there please

@Pierstoval
Copy link
Contributor Author

Yeah, we have identified the issue and will be resolving this and a number of other minor issues over the weekend. Hang in there please

Need help? I might find some time in the weekend 😉 I just don't know where to start 😅

@Laegel Laegel mentioned this issue Apr 16, 2021
12 tasks
@jbolda jbolda closed this as completed in fcb3b48 Apr 19, 2021
@Pierstoval
Copy link
Contributor Author

Awesome! 🚀

Do you have any ETA on when the next release will be published?

@nothingismagick
Copy link
Sponsor Member

In 4-8 hours.

@Pierstoval
Copy link
Contributor Author

Pierstoval commented Apr 19, 2021

In 4-8 hours.

Thanks! Awesome! 👏

@edbrannin
Copy link

edbrannin commented Apr 20, 2021

(whoops, commented on the wrong issue)

@GageBachik
Copy link

For anyone coming from the un-updated docs:
yarn add -D @tauri-apps/api

@KarthikeyanKanniappan
Copy link

Still, I cannot able to import. I am getting this error "TypeError: Module name, '@tauri-apps/api' does not resolve to a valid URL." and @tauri-apps/api is in dev-dep

@FabianLars
Copy link
Member

Adding the api as a dev dependency doesn't make much sense to me tbh. The cli is a dev dependency, but not the api. (that said, it shouldn't reallyyy make a difference, most tools handle dev and normal deps the same).

Can you show how you added the dependency to package.json and how you're trying to import it in js/ts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants