examples/nodejs-threads/README.md
This example demonstrates using wasm-bindgen with Node.js worker_threads for multithreading, supporting both CommonJS and ESM targets.
initSync({ module, memory })npm run build
npm test
When targeting Node.js (CJS or ESM) with threads enabled, wasm-bindgen generates:
initSync(opts) - Initialize the WASM module synchronously__wbg_get_imports(memory) - Get the imports object with optional custom memory__wbg_wasm_module - The compiled WebAssembly.Module for sharing with workers__wbg_memory - The shared WebAssembly.MemoryMain thread auto-initializes on require() or import. Worker threads should call:
// CJS
const wasm = require('./pkg/nodejs_threads.js');
wasm.initSync({
module: workerData.wasmModule, // from main thread
memory: workerData.memory // from main thread
});
// ESM
import { initSync } from './pkg/nodejs_threads.js';
initSync({
module: workerData.wasmModule, // from main thread
memory: workerData.memory // from main thread
});
Full TypeScript declarations are generated for all exports:
import {
initSync,
__wbg_get_imports,
__wbg_wasm_module,
__wbg_memory,
type InitSyncOptions
} from './pkg/nodejs_threads.js';