Skip to content

Runtime

A platform runMain executes an application Effect at process startup, logs unhandled failures, handles termination signals, waits for finalizers, and sets the process exit code.

Running an entry point

// preparation.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
 
effect function main() {
  try Console.log("Preparing export")
  try sleep("100 millis")
  try Console.log("Export complete")
}
 
NodeRuntime.runMain(main())
// Output: Preparing export
// Output: Export complete

Calling main() constructs the Effect without running it; NodeRuntime.runMain executes it. Provide application layers at the edge with NodeRuntime.runMain(Effect.provide(main(), AppLive)).

  • Inside effect code, use try or fork rather than a runner.
  • In tests, use it.effect("...", () => effect { ... }).
  • Use BunRuntime from @effect/platform-bun/BunRuntime on Bun, and NodeRuntime from @effect/platform-node/NodeRuntime on Node.js and Deno.
  • Always import Node modules from subpaths (@effect/platform-node/NodeRuntime), not the barrel @effect/platform-node—the barrel imports NodeRedis, which fails under Yarn Classic with Cannot find package 'redis'.

Launch options and custom teardown

// startup-rejected.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
 
error StartupRejected {
  reason: string
}
 
effect function main() {
  throw new StartupRejected({ reason: "configuration missing" })
}
 
NodeRuntime.runMain(main(), { disableErrorReporting: true })

disableErrorReporting: true suppresses automatic error logging while preserving failure exit codes. Provide a custom teardown callback in TypeScript to override exit-code mapping (keep effectful cleanup inside the Effect itself):

// TypeScript (.ts)
// launch-options.ts
import { Exit, type Runtime } from "effect"
 
export const launchOptions = {
  teardown: ((exit, onExit) => {
    onExit(Exit.isSuccess(exit) ? 0 : 2)
  }) satisfies Runtime.Teardown
}
// custom-entry.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import { launchOptions } from "./launch-options.ts"
 
effect function main() {
  try Console.log("Starting with custom exit handling")
}
 
NodeRuntime.runMain(main(), launchOptions)
// Output: Starting with custom exit handling

Completion, signals, and exit codes

// invalid-batch.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
 
error InvalidBatch {
  count: number
}
 
effect function exportBatch(count: number) {
  try ensuring(effect {
    if (count <= 0) throw new InvalidBatch({ count })
    try Console.log(`Exporting ${count} records`)
  }, Console.log("Export cleanup finished"))
}
 
NodeRuntime.runMain(exportBatch(0))

Default Effect 4.1.1 teardown exit codes:

  • 0: normal return (the return value is not printed).
  • 130: interruption-only cause (including SIGINT / SIGTERM).
  • 1: unhandled typed failure or defect (unless an error supplies its own exit code).

On termination signals, runMain interrupts the root fiber and awaits all ensuring, onExit, and scoped finalizers before exiting. catch handles typed failures without swallowing interruption. To inspect which signal fired, use observeSignals.

Verifying process exit codes

These host-side checks launch each compiled entry in its own Node process to verify silent failure, finalizer execution on error, and custom exit-code mapping:

// TypeScript (.ts)
// runtime-checks.ts
// types: node
import type {} from "./startup-rejected.vibe"
import type {} from "./invalid-batch.vibe"
import type {} from "./custom-entry.vibe"
import { spawn } from "node:child_process"
import { fileURLToPath } from "node:url"
 
// Each check is its own Node process; the four run at the same time.
function launch(...args: string[]) {
  return new Promise<{ status: number | null; stdout: string; stderr: string }>((done, failed) => {
    const child = spawn(process.execPath, args, { cwd: fileURLToPath(new URL(".", import.meta.url)) })
    let stdout = "", stderr = ""
    child.stdout.setEncoding("utf8").on("data", (chunk: string) => { stdout += chunk })
    child.stderr.setEncoding("utf8").on("data", (chunk: string) => { stderr += chunk })
    child.on("error", failed)
    child.on("close", (status) => done({ status, stdout, stderr }))
  })
}
 
Promise.all([
  launch("./startup-rejected.js"),
  launch("./custom-entry.js"),
  launch("./invalid-batch.js"),
  launch("--input-type=module", "-e", `
    import { Effect } from "effect"
    import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
    import { launchOptions } from "./launch-options.js"
    NodeRuntime.runMain(Effect.fail("rejected"), { ...launchOptions, disableErrorReporting: true })
  `)
]).then(([rejected, custom, batch, failedCustom]) => {
  console.log(`rejected: ${rejected.status}, silent: ${rejected.stdout === "" && rejected.stderr === ""}`)
  console.log(`custom: ${custom.status}`)
  console.log(`batch: ${batch.status}, cleaned: ${batch.stdout.includes("Export cleanup finished")}`)
  console.log(`custom failure: ${failedCustom.status}`)
})
// Output: rejected: 1, silent: true
// Output: custom: 0
// Output: batch: 1, cleaned: true
// Output: custom failure: 2

Runtime package compatibility

Pin vibelang and vibelang-runtime to matching versions. The runtime entry provides the compiler's ABI helpers (including error matcher registration) and verifies the installed Effect version at load time.


The matching Effect page: effect.website/docs/v4/platform/runtime