5

I am new at tauri and I have faced the issue with getting data from @tauri-apps/api.

"@tauri-apps/api": "^1.1.0",
"@tauri-apps/cli": "^1.1.1"

This is my React code below:

/index.jsx

import {getTauriVersion} from "@tauri-apps/api/app"
function App() {
   const func = async () => {
       const res = await getTauriVersion()
       return res    
   }
   return (<></>)
}

This is my tauri.conf.json

{
   "build": {
    "beforeDevCommand": "npm run dev",
    "beforeBuildCommand": "npm run build",
    "devPath": "http://localhost:1420",
    "distDir": "../dist",
    "withGlobalTauri": true
  },
  ...
  "tauri": {
     "allowList": {"all": true}
  }
}

And the error is:

Uncaught (in promise) TypeError: window.__TAURI_IPC__ is not a function
unhandledRejection: ReferenceError: window is not defined
at o (file:///C:/test/test/node_modules/@tauri-apps/api/tauri- 
a4b3335a.js:1:100)

2 Answers2

7

The typical 2 sources for this kind of error are:

  1. you're in a SSR environment - This could be a problem with next.js for example, but not with plain React.
  2. you're viewing the frontend in your browser - Tauri's APIs are only injected into actual Tauri windows.
FabianLars
  • 351
  • 2
  • 4
  • I guess the solution is just not to use Next.JS for Tauri? I'll try using ViteJS instead. – C.Tale Dec 23 '22 at 17:14
  • @C.Tale That would probably be the easiest solution, but it's not necessary per se. You just need to be careful because some nextjs features don't work when you use `next export` and you need to use dynamic imports for the window and path modules to make sure they are only imported on the client-side, etc. – FabianLars Dec 24 '22 at 09:49
0

You can write a function to judge

const handleIsTauri = () => {
    return Boolean(
  typeof window !== 'undefined' &&
  window !== undefined &&
  window.__TAURI_IPC__ !== undefined
)};