2

I am working with Node.js 22 using native ESM and Worker Threads, I'm trying to share a single WebAssembly.Memory instance across multiple workers to avoid duplicating memory.

// main.mjs
import { Worker } from 'node:worker_threads';

const sharedMemory = new WebAssembly.Memory({ shared: true, initial: 10 });

new Worker(new URL('./worker.mjs', import.meta.url), {
  workerData: { memory: sharedMemory }
});
new Worker(new URL('./worker.mjs', import.meta.url), {
  workerData: { memory: sharedMemory }
});


// worker.mjs
import { workerData } from 'node:worker_threads';
import fs from 'node:fs';

const wasmBytes = fs.readFileSync('./module.wasm');
const { instance } = await WebAssembly.instantiate(wasmBytes, {
  env: { memory: workerData.memory }
});

console.log(instance.exports.doSomething());

Here Node.js is throwing a error

TypeError: WebAssembly.Memory(): shared memory cannot be cloned or transferred

Question: How can I safely share a single WebAssembly.Memory instance between multiple ESM worker threads in Node.js 22 is this currently unsupported or does it require a specific initialization pattern ?

1
  • 1
    I'm getting a different error with Node.js v22.14.0 ("If shared is true, maximum property should be defined"), but after fixing that, it seems to work fine. Commented Oct 28 at 13:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.