The @surrealdb/wasm package is a plugin for the JavaScript SDK that allows you to run SurrealDB as an embedded database within a browser environment. It supports in-memory databases and persistent storage via IndexedDB, and can optionally run inside a Web Worker to keep the main thread responsive.
ImportantThis package works with ES modules (
import), not CommonJS (require).
Before installing the WASM engine, you need the core JavaScript SDK installed. Then add the @surrealdb/wasm package.
npm install --save @surrealdb/wasm
yarn add @surrealdb/wasm
pnpm install @surrealdb/wasm
Import createWasmEngines from @surrealdb/wasm and spread it into the engines option alongside createRemoteEngines from the core SDK. This registers the embedded protocols while keeping remote connections available.
import { Surreal, createRemoteEngines } from 'surrealdb'; import { createWasmEngines } from '@surrealdb/wasm'; const db = new Surreal({ engines: { ...createRemoteEngines(), ...createWasmEngines(), }, });
Once the WASM engine is registered, you can connect to SurrealDB using the embedded protocols.
| Protocol | Description |
|---|---|
mem:// | In-memory database (data is lost when the page reloads) |
indxdb:// | Persistent database backed by IndexedDB |
await db.connect('mem://'); await db.connect('indxdb://myapp');
Remote protocols (ws://, wss://, http://, https://) remain available when createRemoteEngines() is also registered.
The WASM engine can run inside a Web Worker to offload database operations from the main thread. This keeps your interface responsive during computationally intensive queries.
import { Surreal, createRemoteEngines } from 'surrealdb'; import { createWasmWorkerEngines } from '@surrealdb/wasm'; import WorkerAgent from '@surrealdb/wasm/worker?worker'; const db = new Surreal({ engines: { ...createRemoteEngines(), ...createWasmWorkerEngines({ createWorker: () => new WorkerAgent(), }), }, }); await db.connect('mem://');
If you are using a bundler like Vite, you may need to exclude the WASM package from dependency optimization and enable top-level await.
vite.config.jsexport default { optimizeDeps: { exclude: ['@surrealdb/wasm'], esbuildOptions: { target: 'esnext', }, }, esbuild: { supported: { 'top-level-await': true, }, }, };
@surrealdb/wasm on npm