Trouble deploying Sveltekit + Rust Wasm project to Vercel

I am unable to get WASM to work on Vercel. I have a normal Sveltekit project, with a Rust library inside. I’m using wasm-pack to compile and optimize it. This is the current way I’m using it inside the main +page.svelte:

import { onMount } from 'svelte';
let lib: typeof import('wasm-lib');

onMount(async () => {
	lib = await import('wasm-lib');
	await lib.default();

	let largeArray = new Int32Array(1e6).fill(1);
	console.log(lib.sum(largeArray));
});

And it does indeed work when ran locally, using bun run dev, however, not on Vercel. The error messages I am getting are:

using deprecated parameters for the initialization function; pass a single object instead

GET https://dcutils.vercel.app/_app/immutable/chunks/wasm_lib_bg.wasm 404 (Not Found)

`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:
 TypeError: Failed to execute 'compile' on 'WebAssembly': HTTP status code is not ok

Uncaught (in promise) CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 4e 6f 74 20 @+0

Hi, @devtac2! Welcome to the Vercel Community :smile:

Have you read our docs on using WASM?

It would help if you could share a minimal reproducible example with us to dig deeper. Thanks!

Of course. However, I’ve fixed the issue. I still do not know why, but importing from node_modules does not work. So, I’ve modified my bun run wasm command so that it moves the generated pkg directory into $lib. Then, I import and use my functions like so:

import init, { get_results } from '$lib/pkg';

onMount(async () => {
	await init();
	get_results();
}

Sorry for troubling you, and thank you for your time!

1 Like

Not at all, @devtac2! Feel free to come back with anymore questions :smile:

Also, would love to see your project showcased in Community if you want to!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.