Engine API (@tevm/node)
@tevm/node (re-exported from tevm and tevm/node) is the lowest-level JavaScript interface in TEVM. Use it when building custom JSON-RPC servers, proxies, CLI harnesses, or WASM integrations.
Reference
Section titled “Reference”Exports
Section titled “Exports”createZevmEngine(options?: ZevmEngineOptions): ZevmEnginecreateTevmNode— alias forcreateZevmEnginecreateWasmBackend(options?: ZevmWasmOptions): Promise<ZevmBackend>NativeRpcError— structured JSON-RPC error class (code: number,message: string,data?: JsonValue)- Types:
ZevmEngine(TevmNode),ZevmEngineOptions(TevmNodeOptions),ZevmBackend,ZevmWasmOptions,ZevmWasmSource,ZevmEngineEvents,EngineEventEmitter,EngineRequest,JsonValue,ConsoleLogEvent,ConsoleLogHandler
ZevmEngine
Section titled “ZevmEngine”export type JsonValue = | null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }
export type EngineRequest = { method: string params?: JsonValue[]}
export type ZevmEngine = { events: EngineEventEmitter<ZevmEngineEvents> ready: () => Promise<void> request: (request: EngineRequest) => Promise<JsonValue> rpc: (json: string) => Promise<string | null> close: () => Promise<void>}ZevmEngineOptions
Section titled “ZevmEngineOptions”| Option | Type | Default | Description |
|---|---|---|---|
chainId |
number |
common?.id ?? 31337 |
Unsigned safe integer chain ID. With an injected backend, startup fails if the backend’s eth_chainId differs. |
common |
{ id: number } |
undefined |
Supplies chainId from common.id. Throws synchronously if both chainId and common.id are provided and differ. |
mining |
{ auto?: boolean; interval?: number } |
{ auto: true } |
Automine flag and optional background interval in whole seconds (0 disables timer). |
fork |
{ url: string; blockNumber?: number } |
undefined |
Fork upstream state into a local chain starting at block 0. |
backend |
ZevmBackend |
undefined |
Custom { rpc, close } JSON-RPC backend. Mutually exclusive with wasm. |
wasm |
boolean | ZevmWasmSource |
auto outside Node.js | Built-in @evmts/zevm/browser WASM reactor control (true, ArrayBuffer, ArrayBufferView, WebAssembly.Module, or false). Mutually exclusive with backend. |
consoleLog |
boolean | ConsoleLogHandler |
false |
true prints Solidity console.log via console.log; a function receives (message, event). |
ZevmBackend & ZevmWasmOptions
Section titled “ZevmBackend & ZevmWasmOptions”export type ZevmBackend = { rpc: (json: string) => string | null | Promise<string | null> close: () => void | Promise<void>}
export type ZevmWasmSource = ArrayBuffer | ArrayBufferView | WebAssembly.Module
export type ZevmWasmOptions = { chainId?: number wasm?: ZevmWasmSource}ZevmEngineEvents
Section titled “ZevmEngineEvents”export type ZevmEngineEvents = { request: [request: EngineRequest] response: [response: { request: EngineRequest; result: JsonValue }] block: [block: JsonValue] consoleLog: [event: ConsoleLogEvent] error: [error: unknown] close: []}events is a zero-dependency EngineEventEmitter (not node:events EventEmitter) so @tevm/node stays browser-safe.
NativeRpcError
Section titled “NativeRpcError”Extends Error with:
code: number— exact JSON-RPC error code from the backend (e.g.3for revert,-32601Method not found,-32602Invalid params,-32000Execution/admission error).data?: JsonValue— optional structured payload (e.g. revert bytes'0x...').
request() vs rpc()
Section titled “request() vs rpc()”import { createZevmEngine, NativeRpcError } from 'tevm/node'
const engine = createZevmEngine({ mining: { auto: false } })
try { await engine.ready()
// EIP-1193 style: returns parsed result or throws NativeRpcError const chainId = await engine.request({ method: 'eth_chainId' }) console.log(chainId) // '0x7a69'
// Raw wire JSON-RPC: single request, batch array, or notification (returns null) const batch = await engine.rpc( JSON.stringify([ { jsonrpc: '2.0', id: 1, method: 'eth_blockNumber' }, { jsonrpc: '2.0', id: 2, method: 'eth_accounts' }, ]), ) console.log(JSON.parse(batch!))
const notification = await engine.rpc( JSON.stringify({ jsonrpc: '2.0', method: 'eth_chainId' }), ) console.log(notification) // null
// Structured error handling try { await engine.request({ method: 'missing_method' }) } catch (error) { if (error instanceof NativeRpcError) { console.log(error.code, error.message) // -32601 'Method not found' } }} finally { await engine.close()}Explicit WASM backend
Section titled “Explicit WASM backend”import { createWasmBackend, createZevmEngine } from 'tevm/node'
const backend = await createWasmBackend({ chainId: 31337, wasm: wasmBytes })const engine = createZevmEngine({ backend, chainId: 31337 })try { await engine.ready() console.log(await engine.request({ method: 'eth_chainId' })) // '0x7a69'} finally { await engine.close()}Troubleshooting
Section titled “Troubleshooting”Error('engine is closed'): You calledrequest()orrpc()afterclose()(or after startup failed and automatically closed the handle).- Transport fault instead of
NativeRpcError: A custombackend.rpc()returned JSON missing a valid JSON-RPC 2.0 envelope (must have exactly one ofresultorerror, whereerrorhas integercodeand stringmessage). backendandwasmconflict: Passing both options throws synchronously. Remove one.

