Skip to content

[bug] TypeError: Cannot read properties of undefined (reading '__currentWindow') #3554

Closed

Description

@astral-horizon-labs

Describe the bug

Trying to update my app to latest Tauri I get this error TypeError: Cannot read properties of undefined (reading '__currentWindow').

This is all the @tauri-apps/api imports I have in my app:
import { once } from '@tauri-apps/api/event';
import { exit } from '@tauri-apps/api/process';
import type { Child } from '@tauri-apps/api/shell';
import { Command } from '@tauri-apps/api/shell';
import { writeFile, readTextFile, removeFile, readDir, createDir } from '@tauri-apps/api/fs';

I did guard all calls with if ('__TAURI__' in window) { } but it's not enough.

Reproduction

No response

Expected behavior

No response

Platform and versions

▶ cargo tauri info

Operating System - Mac OS, version 12.2.1 X64

Node.js environment
  Node.js - 16.14.0
  @tauri-apps/cli - 1.0.0-rc.5
  @tauri-apps/api - 1.0.0-rc.1

Global packages
  npm - 8.1.4
  pnpm - Not installed
  yarn - 1.22.17

Rust environment
  rustup - 1.24.3
  rustc - 1.58.1
  cargo - 1.58.0
  toolchain - stable-aarch64-apple-darwin 

App directory structure
/node_modules
/public
/scripts
/.github
/src-tauri
/.git
/.idea
/src

App
  tauri - 1.0.0-rc.3
  tauri-build - 1.0.0-rc.3
  tao - 0.6.2
  wry - 0.13.2
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../build
  devPath - http://localhost:3003/
  framework - React
  bundler - Webpack

Stack trace

No response

Additional context

No response

Activity

  1. amrbashir commented on Feb 24, 2022

    @amrbashir
    Member

    Please provide a minimal repro.

  2. astral-horizon-labs commented on Feb 28, 2022

    @astral-horizon-labs
    Author

    Hello! I was able to narrow down the problem. It comes from the path module.
    In this minimal reporduction made with create-tauri-app I call path.appDir(). You can see in the console the error above.
    https://github.com/Janaka-Steph/tauri-app-path

  3. amrbashir commented on Feb 28, 2022

    @amrbashir
    Member

    Your minimal repro had wrong usage of async function inside jsx.
    your App.tsx should be like this

    function App() {
      const [appDir, setAppDir] = useState('');
      path.appDir()
        .then(v => setAppDir(v))
        .catch(e => console.error(e));
    	
      return <div className="App">
        <p>{appDir}</p> 
      </div>
    }

    I am not a good react dev so there might be a better way to do this.
    Anyways it works fine with the above snippet.
    image

  4. marc2332 commented on Feb 28, 2022

    @marc2332
    Member

    Just like @amrbashir but a bit better :)

    function App() {
      const [appDir, setAppDir] = useState<string | null>(null);
      
      useEffect(() => {
        path.appDir().then(dir => setAppDir(dir));
      }, [])
    
      return (
        <div className="App">
          <header className="App-header">
            <div className="inline-logo">
              <img src={tauriCircles} className="App-logo rotate" alt="logo" />
              <img src={tauriWord} className="App-logo smaller" alt="logo" />
            </div>
            <h1>App Directory</h1>
            <p>{appDir !== null ? appDir : 'loading...'}</p>
          </header>
        </div>
      )
    }
  5. astral-horizon-labs commented on Feb 28, 2022

    @astral-horizon-labs
    Author

    Unfortunately changing the code like you did didn't solve the issue for me.
    I am using a MacBook M1, maybe it's related to that.

  6. FabianLars commented on Feb 28, 2022

    @FabianLars
    Member

    We're kinda missing the actual problem here. The issue we were initially talking about was that importing X* crashes the frontend in browser even if the function call itself is guarded with if (window.__TAURI__).

    • with X now being identified as path.appDir() or the path module in general
      Change it to:
    useEffect(() => {
            if (window.__TAURI__) {
                path.appDir().then(dir => {
                    console.log('dir', dir);
                    setAppDir(dir);
                });
            }
        }, []);

    run the app, open it in the brwoser and check the console log

  7. marc2332 commented on Feb 28, 2022

    @marc2332
    Member

    I believe the issue is:

    const appWindow = new WebviewWindow(

    image

    Solution: dynamic import 🤔?

    something like:

    if (window.__TAURI__){
      const { path } = await import('@tauri-apps/api')
    }

    or maybe just refactor tauri/api/window to avoid having that module-scoped variable

  8. astral-horizon-labs commented on Feb 28, 2022

    @astral-horizon-labs
    Author

    Sorry my issue was not precise enough. I need to be able to run the app in both Tauri and web version.
    I added a guard in this repro in useEffect as @FabianLars sugested.
    It works with cargo tauri dev but with yarn start I get the error.

  9. astral-horizon-labs commented on Feb 28, 2022

    @astral-horizon-labs
    Author

    And as @marc2332 suggested, dynamic import fix the issue.
    But would be nice if I can keep regular imports.

      useEffect(() => {
        (async () => {
          if ('__TAURI__' in window) {
            const {path} = await import('@tauri-apps/api')
            path.appDir().then(dir => {
              console.log('dir', dir);
              setAppDir(dir)
            });
          }
        })()
      }, [])
    
  10. lucasfernog commented on Feb 28, 2022

    @lucasfernog
    Member

    This is a really weird use case lol I thought it was an issue with Tauri itself.

    You could move all Tauri API usage to a single file and conditionally import that, I do this at work using Webpack. The easy solution would be changing your HTML file to initialize window.__TAURI_METADATA__ with a dummy value.

  11. lucasfernog commented on Feb 28, 2022

    @lucasfernog
    Member

    We'll help you here #3572

  12. added a commit that references this issue on Mar 3, 2022
    9cb1059
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions