WebAssembly build
Each Node.js package has a WebAssembly build. Use it in an environment where the native addon cannot load, for example StackBlitz WebContainers, or on a platform that has no native package.
Packages
The WebAssembly build of each package is a separate package with the
-wasm32-wasi suffix. It is built for the wasm32-wasip1-threads target.
The main package lists the WebAssembly package in optionalDependencies, with
the CPU wasm32. A package manager installs it only when you ask for the
wasm32 CPU:
-
npm:
npm install @swc/anyformat-image --cpu=wasm32 -
pnpm: add
wasm32tosupportedArchitecturesinpnpm-workspace.yaml:
Import the main package as usual. Do not import the WebAssembly package directly.
How the package selects the build
The package loads the native addon first. When no native addon loads, it loads
the WebAssembly build. The environment variable NAPI_RS_FORCE_WASI changes
this order:
Other values, for example 1, have no effect.
Requirements
- Node.js 20.19, 22.13, 23.5, or later. The native build supports Node.js 18 and later.
SharedArrayBufferand worker threads. The WebAssembly build runs its thread pool in worker threads.
Node.js prints ExperimentalWarning: WASI is an experimental feature when the
package loads the WebAssembly build.
Limits
- The WebAssembly build is slower than the native build.
- The file functions, for example
detectFile, can read only the files on the drive of the working directory. This applies to Windows. - An I/O error of a file function has the same
codeas in the native build, for exampleENOENT. When you give a relative path, thepathproperty is the absolute path. - Browsers are not supported. The main package uses Node.js modules, for
example
node:fs. - Cloudflare Workers and other runtimes without
SharedArrayBufferor worker threads are not supported. - The Python packages have no WebAssembly build.