rive-app / rive-wasm

Wasm/JS runtime for Rive
MIT License
660 stars 46 forks source link

Vite: "Uncaught TypeError: Cannot read properties of undefined (reading 'Rive')" #342

Closed mandrasch closed 5 months ago

mandrasch commented 6 months ago

Description

Hi! I'm just getting started with Rive.

I wanted to create a simple demo with Vite (vanilla-ts) and I ran into the following error locally:

Uncaught TypeError: Cannot read properties of undefined (reading 'Rive')
    at main.ts:30:22

Line 30, main.ts:

const canvas = document.getElementById("canvas") as HTMLCanvasElement|null; 
if(canvas !== null){
  const r = new rive.Rive({

I found another post on Discord, same error: Discord Post

I suspect this is because I'm trying to play a Rive file before the WASM file is loaded. But how do I check if that file is loaded?

Provide a Repro

The production build works fine though: https://rive-vite-demo.vercel.app/

Source .riv/.rev file

Expected behavior

Screenshots

image

Browser & Versions (please complete the following information)

Additional context

Thanks in advance for any hints! ๐Ÿ‘‹ Cheers! ๐Ÿ™‚

Aratmany commented 6 months ago

Hi, you can try doing it like this and it will work fine

  1. change import like this Screen Shot 2024-01-04 at 15 57 26
  2. change this code to const r = new rive.Rive changes to this => const r = new Rive image
mandrasch commented 6 months ago

Thanks very much, @Aratmany!

Updated my repo. Works great! ๐Ÿฅณ

Info:

I followed this guide here (https://help.rive.app/runtimes/overview/web-js#id-1.-install-the-dependency), maybe an info box for Vite users would be helpful when getting started with Rive. Cheers!

zplata commented 6 months ago

Thanks for the repro! Was trying to dig into Vite docs on why this might be an issue. First guess is that it has to do with Vite serving as ESM, and having issues with default imports on UMD builds. But we'll work on adding a blurb in the web docs to make this clearer for Vite config.

mandrasch commented 6 months ago

๐Ÿ‘ you're welcome!

I'm not a pro in the field of ESM vs UMD unfortunately. Would be cool to find out what's going on.


Maybe Dep Optimization Options and Dependency Pre-Bundling is also relevant.

Also tried it with this Vite setting

 optimizeDeps: {
    include: ["@rive-app/canvas"],
  },

But

import rive from "@rive-app/canvas";
let layout = new rive.Layout({
// etc.

also still gave Vite: "Uncaught TypeError: Cannot read properties of undefined (reading 'Rive') with that in the browser.


Btw: I just added Layout as well and needed to use

  import { Rive, Layout, Fit } from "@rive-app/canvas";
  // ...
  let layout = new Layout({
    fit: Fit.Cover,
  });  

instead of the functions described for Web in https://help.rive.app/runtimes/layout like let layout = new rive.Layout({

So it affects all functions described in the docs.

zplata commented 6 months ago

Hey @mandrasch - had to take a step back and refresh on what the main exports are here, but this is a docs issue mainly. The new rive.Rive({...}) occasionally used in code snippets across docs comes from rive being exposed as a global variable if you include the unpkg script to pull in the library in plain HTML/JS projects, for example.

When importing the high-level @rive-app/canvas library however, you'll need to import the named exports to use the API, so like it was suggested above (and as you discovered with the Layout scenario too), you'll need to do:

import {Rive, Layout, Fit, Alignment} from "@rive-app/canvas";

or alternatively, if you want a "default import" of a sort, you could do:

import * as rive from "@rive-app/canvas";

We updated docs now to reflect this clarification

mandrasch commented 5 months ago

Hi @zplata,

ah, makes sense. Thanks very much for explanation! ๐Ÿ‘ ๐Ÿ‘