Skip to content

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.

  • createZevmEngine(options?: ZevmEngineOptions): ZevmEngine
  • createTevmNode — alias for createZevmEngine
  • createWasmBackend(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
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>
}
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).
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
}
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.

Extends Error with:

  • code: number — exact JSON-RPC error code from the backend (e.g. 3 for revert, -32601 Method not found, -32602 Invalid params, -32000 Execution/admission error).
  • data?: JsonValue — optional structured payload (e.g. revert bytes '0x...').
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()
}
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()
}
  • Error('engine is closed'): You called request() or rpc() after close() (or after startup failed and automatically closed the handle).
  • Transport fault instead of NativeRpcError: A custom backend.rpc() returned JSON missing a valid JSON-RPC 2.0 envelope (must have exactly one of result or error, where error has integer code and string message).
  • backend and wasm conflict: Passing both options throws synchronously. Remove one.