#Cannot import { emit, listen } event

17 messages · Page 1 of 1 (latest)

crude meteor
#

Tauri site recommends

import { emit, listen } from "@tauri-apps/api/event";

I also tried

const { emit, listen } = require("@tauri-apps/api/event");

Not sure what to do now

drifting pond
#

You don't have a bundler.

crude meteor
#

Require is suppose to work still no?

drifting pond
#

No, that's not valid ECMAScript.

crude meteor
#

So I need to remake project with TS instead of JS?

#

If I understand properly

drifting pond
#

If you're starting from scratch, I recommend using TypeScript because it will save you hours of console.debugging.

crude meteor
#

Aight, back to TS I go then, better restart now

#

Thanks for the help btw

loud perch
#

the browser understands esm just fine, I don't see why you'd need typescript for this 🤔

drifting pond
#

@tauri-apps/api/event isn't ESM. It's a Node.js library.

loud perch
drifting pond
#

If you're not using Node.js, one option is to enable withGlobalTauri and reference it as const { emit, listen } = window.__TAURI__.event; which is the instructions on the website as I mentioned earlier. The other option is to manually copy the ESM files from the Node.js library which makes far less sense since you still have to install Node.js to get the files and then copy or symlink the files into your src folder.

#

I did say that TypeScript is not required. It has never been a requirement for developing with Tauri. I recommend TypeScript because it will tell you about type errors and provides offline documentation. Without TypeScript, you're constantly visiting https://tauri.app/v1/api/js or reading the Rust source code to find out the types that the API accepts and returns.

#

I don't recommend it for large existing projects as the benefits do not scale with the work it takes to migrate and add proper references to each file if your code is split into multiple files that made assumptions about the previous file being loaded (this was a very common practice before ESM).