> For AI agents: the complete documentation index is available at https://anyformat.swc.rs/llms.txt, the full documentation bundle is available at https://anyformat.swc.rs/llms-full.txt.

# 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.

| Package                 | WebAssembly package                 |
| ----------------------- | ----------------------------------- |
| `@swc/anyformat`        | `@swc/anyformat-wasm32-wasi`        |
| `@swc/anyformat-image`  | `@swc/anyformat-image-wasm32-wasi`  |
| `@swc/anyformat-audio`  | `@swc/anyformat-audio-wasm32-wasi`  |
| `@swc/anyformat-video`  | `@swc/anyformat-video-wasm32-wasi`  |
| `@swc/anyformat-font`   | `@swc/anyformat-font-wasm32-wasi`   |
| `@swc/anyformat-pdf`    | `@swc/anyformat-pdf-wasm32-wasi`    |
| `@swc/anyformat-office` | `@swc/anyformat-office-wasm32-wasi` |

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 `wasm32` to `supportedArchitectures` in `pnpm-workspace.yaml`:

  ```yaml
  supportedArchitectures:
    cpu:
      - current
      - wasm32
  ```

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:

| Value   | Behavior                                                          |
| ------- | ----------------------------------------------------------------- |
| Not set | Use the native addon. Use the WebAssembly build only as fallback. |
| `true`  | Use the WebAssembly build. Use the native addon as fallback.      |
| `error` | Use the WebAssembly build. Throw an error when it does not load.  |

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.
- `SharedArrayBuffer` and 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 `code` as in the native build,
  for example `ENOENT`. When you give a relative path, the `path` property 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 `SharedArrayBuffer` or worker
  threads are not supported.
- The Python packages have no WebAssembly build.
