Skip to content

Browsers & WebAssembly

@tevm/node and @tevm/memory-client contain zero static Node.js built-in imports and bundle cleanly for platform: 'browser'. You can run an in-process EVM directly inside browsers and workers with zero configuration, load the zevm.wasm reactor explicitly, or inject your own JSON-RPC backend.

When @evmts/zevm is installed, calling createMemoryClient() or createZevmEngine() outside Node.js automatically loads @evmts/zevm/browser’s compiled zevm.wasm reactor during initialization.

Use a browser bundler (such as Vite) that serves static .wasm assets referenced via new URL('./zevm.wasm', import.meta.url):

src/browser-evm.ts
import { createMemoryClient, parseAbi } from 'tevm'
export async function runInBrowser() {
// Automatically loads @evmts/zevm/browser's zevm.wasm outside Node.js
const client = createMemoryClient()
try {
const deployed = await client.tevmDeploy({
bytecode: '0x600a600c600039600a6000f3602a60005260206000f3',
})
const result = await client.tevmContract({
address: deployed.createdAddress!,
abi: parseAbi(['function answer() view returns (uint256)']),
functionName: 'answer',
})
return result.data // 42n
} finally {
await client.tevmClose()
}
}

Pass wasm as an ArrayBuffer, TypedArray (ArrayBufferView), or compiled WebAssembly.Module to force the WebAssembly backend in any environment — including browsers with custom asset URLs, Web Workers, Cloudflare Workers, Deno, Bun, and Node.js.

  1. explicit-wasm.ts
    import { createMemoryClient } from 'tevm'
    const response = await fetch('/assets/zevm.wasm')
    const wasmBytes = await response.arrayBuffer()
    const client = createMemoryClient({
    chainId: 31337,
    wasm: wasmBytes,
    })
    try {
    console.log(await client.getChainId()) // 31337
    } finally {
    await client.tevmClose()
    }
  2. Or construct a reusable ZevmBackend with createWasmBackend

    Section titled “Or construct a reusable ZevmBackend with createWasmBackend”

    Use createWasmBackend() when you want explicit async initialization before creating the engine or sharing a pre-compiled WebAssembly.Module across isolated instances:

    wasm-backend.ts
    import { createMemoryClient, createWasmBackend } from 'tevm'
    const module = await WebAssembly.compileStreaming(fetch('/assets/zevm.wasm'))
    const backend = await createWasmBackend({
    chainId: 31337,
    wasm: module,
    })
    const client = createMemoryClient({ backend, chainId: 31337 })
    try {
    console.log(await client.getBlockNumber()) // 0n
    } finally {
    // Closes the backend and frees the WASM instance
    await client.tevmClose()
    }

Implement the two-method ZevmBackend interface ({ rpc, close }) to plug any custom WASM EVM, Web Worker bridge, or remote JSON-RPC runtime into createZevmEngine() or createMemoryClient() without loading @evmts/zevm at all:

custom-backend.ts
import { createMemoryClient, type ZevmBackend } from 'tevm'
function createWorkerBackend(worker: Worker): ZevmBackend {
let nextId = 1
const pending = new Map<number, (response: string | null) => void>()
worker.addEventListener('message', (event: MessageEvent<{ id: number; response: string | null }>) => {
const resolve = pending.get(event.data.id)
if (resolve) {
pending.delete(event.data.id)
resolve(event.data.response)
}
})
return {
rpc(json) {
const id = nextId++
return new Promise((resolve) => {
pending.set(id, resolve)
worker.postMessage({ id, json })
})
},
close() {
worker.terminate()
},
}
}
const client = createMemoryClient({
backend: createWorkerBackend(new Worker(new URL('./evm-worker.ts', import.meta.url), { type: 'module' })),
})

rpc(json) receives one raw JSON-RPC 2.0 string (single request, batch array, or notification) and returns the serialized JSON response string — or null for notifications. Both synchronous and asynchronous (Promise) implementations are supported.

Option passed Runtime behavior
(neither backend nor wasm) Node.js >= 22.12 loads @evmts/zevm native Node-API addon; browsers/workers auto-load @evmts/zevm/browser WASM reactor.
wasm: true Forces @evmts/zevm/browser WASM reactor (auto-fetches ./zevm.wasm relative to the module URL).
wasm: bytes | WebAssembly.Module Forces the WASM reactor using your supplied binary or compiled module in any runtime.
wasm: false Disables automatic WASM fallback outside Node.js; throws if the native addon is unavailable.
backend: { rpc, close } Uses your custom backend directly without touching @evmts/zevm.

Connecting a browser to a local native server instead

Section titled “Connecting a browser to a local native server instead”

If your browser app needs native Node.js speed, mainnet forking without browser CORS limits, or shared state across tabs, run @tevm/server in Node.js and connect from the browser over HTTP or WebSocket using createHttpClient or viem’s http() / webSocket() transports.