Skip to content

Introduction to Effect Platform

VibeLang accesses files, paths, child processes, HTTP, crypto, and terminals through Effect's platform services. Application modules depend only on abstract services; the entry module provides the concrete runtime layer and starts main().

Platform and HTTP server layer stackPlatform and HTTP server layer stack
Platform and HTTP server layer stack

Host boundaries (EK4402)

Use Effect services for host APIs in .vibe code. For example, use FileSystem for files and HttpClient for requests. Direct host access such as fetch or a node:fs value import reports EK4402. If no service covers an API, put the host call in a TypeScript module and call it through Effect.try or Effect.tryPromise. See Platform globals for replacements and the specification for the full rules.

Using platform services

// main.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import * as NodeServices from "@effect/platform-node/NodeServices"
 
export effect function main() {
  try scoped(effect {
    const fs = try FileSystem.FileSystem
    const path = try Path.Path
    const spawner = try ChildProcessSpawner.ChildProcessSpawner
    const directory = try fs.makeTempDirectoryScoped()
    const file = path.join(directory, "greeting.txt")
    try fs.writeFileString(file, "Hello, VibeLang!")
    try Console.log(try fs.readFileString(file))
    const output = try spawner.string(ChildProcess.make("echo", ["from a child process"]))
    try Console.log(output.trim())
  })
}
 
NodeRuntime.runMain(Effect.provide(main(), NodeServices.layer))
// Output: Hello, VibeLang!
// Output: from a child process

try FileSystem.FileSystem reads the service from R; try fs.readFileString(file) runs the operation and adds PlatformError to E. Module exports such as FileSystem.layerNoop keep their normal meaning (Effect's modules). scoped releases the temporary directory and any spawned ChildProcessSpawner handles when the block completes, fails, or is interrupted.

Shared modules declare effect functions and leave platform selection to the entry module or test; the entry calls NodeRuntime.runMain(Effect.provide(main(), NodeServices.layer)).

Replacing Node.js and web calls

Effect and experimental platform services are in scope without imports; EK4402 names the replacement and its layer. Date.now and Math.random remain allowed ECMAScript APIs, with testable Effect replacements listed below:

CallEffectLayer
fs.readFiletry (try FileSystem.FileSystem).readFile(path)NodeServices.layer
os.tmpdirtry (try Experimental.Process.Process).temporaryDirectoryExperimentalNodeServices.layer
net.isIPv6Experimental.NetAddress.isIPv6(address)none (pure)
child_process.spawn with inherited stdiotry (try Experimental.ChildProcess.ChildProcess).runInherited(command, args, options)ExperimentalNodeChildProcess.layer
new Headers(headers)try (try Experimental.Headers.Headers).copy(headers)Node, Bun or browser experimental layer
Readable.fromWeb(response.body)try (try Experimental.NodeStream.NodeStream).fromWeb(body, { response, signal })Node or Bun experimental layer
new Request(url, request)try (try Experimental.Request.Request).atUrl(url, request)Node, Bun or browser experimental layer
path.join(try Path.Path).join(directory, name)NodeServices.layer
process.cwd(try Path.Path).resolve()NodeServices.layer
child_process.execFileSynctry (try ChildProcessSpawner.ChildProcessSpawner).string(ChildProcess.make(command, args))NodeServices.layer
fetchtry HttpClient.get(url)FetchHttpClient.layer
http.createServertry Layer.launch(HttpRouter.serve(app))ExperimentalNodeHttpServer.layer({ port })
crypto.randomUUIDtry (try SecureRandom.SecureRandom).uuidV4()NodeCrypto.layer
crypto.subtle.digesttry (try Digest.Digest).digest(Digest.sha256, bytes)NodeCrypto.layer
process.argv.slice(2)try (try Stdio.Stdio).argsNodeServices.layer
process.getuid?.()try (try Experimental.Process.Process).currentUidExperimentalNodeServices.layer
os.hostnametry (try Experimental.Process.Process).hostnameExperimentalNodeServices.layer
os.homedirtry (try Experimental.Process.Process).homeDirectoryExperimentalNodeServices.layer
process.killtry (try Experimental.Process.Process).kill(pid, { killSignal })ExperimentalNodeServices.layer
fs.constants.X_OKtry (try Experimental.FileSystem.FileSystem).access(path, { executable: true })ExperimentalNodeServices.layer
crypto.createHmactry (try Mac.Mac).sign(key, data)NodeCrypto.layer
navigator.lockstry (try Experimental.Locks.Locks).hold(name)ExperimentalBrowserLocks.layer
process.envtry Config.String("NAME")none
console.logtry Console.log(message)none
setTimeouttry sleep("1 second")none
Date.nowtry Clock.currentTimeMillisnone
performance.now() - startNumber((try Clock.monotonicTimeNanos) - startNanos) / 1e6none
Math.randomtry Random.nextnone

Key notes:

  • Bytes vs. UTF-8: readFile(path) returns bytes; use readFileString(path) for UTF-8 text.
  • Arguments: Stdio.args matches process.argv.slice(2). Use a .ts adapter if you need the executable or script path.
  • Monotonic time: capture const startNanos = try Clock.monotonicTimeNanos before the work and subtract it from a later reading of the same clock. This clock has an arbitrary origin, which can differ from performance.now() on Node; keep native performance.now() reads in a .ts adapter when you rely on performance.timeOrigin.
  • Bundles: NodeServices.layer (and BunServices.layer) provides FileSystem, Path, Stdio, Terminal, and ChildProcessSpawner. Effect 4.1 crypto services (SecureRandom, Digest, Mac, Signature) come from NodeCrypto.layer, BunCrypto.layer, or BrowserCrypto.layer.
  • Experimental services: VibeLang adds services Effect does not ship yet under Experimental.* (Experimental platform APIs), backed by ExperimentalNodeServices.layer, -bun, or -browser from vibelang-runtime/experimental/platform-*. See Platform globals for the complete Node.js and web catalog.

Making an HTTP client request

FetchHttpClient.layer supplies Effect 4's HttpClient (not included in NodeServices.layer). All three modules below are in scope without imports:

effect function requestJson(url: string) {
  provide FetchHttpClient.layer {
    const response = try HttpClient.get(url)
    return try response.json
  }
}
 
export effect function main() {
  try Console.log(try requestJson("https://jsonplaceholder.typicode.com/todos/1"))
}
 
await runPromise(main())

Both HttpClient.get and response.json can fail in E. Decode the returned unknown JSON with a Schema before trusting its fields.

Installing runtime-specific packages

Follow Install the packages for npm, Bun, pnpm, and Yarn commands. Keep effect and every @effect/platform-* package on matching stable ^4.1.0 releases (tested on 4.1.6; no @effect/platform-node-shared override is required):

  • Node.js / Deno: @effect/platform-node@^4.1.6
  • Bun: @effect/platform-bun@^4.1.6
  • Browser: @effect/platform-browser@^4.1.6

Compile .vibe and .ts sources with npx vibelang (npx vibelang --noEmit to type-check only), then execute the emitted JavaScript—never pass .vibe files directly to a TypeScript runner.

Writing platform-independent programs

// greeting.vibe
export effect function greet() {
  const fs = try FileSystem.FileSystem
  const greeting = try fs.readFileString("./greeting.txt")
  try Console.log(greeting.trim())
}
// Run in an effect function: provide FileSystem.layerNoop({ readFileString: () => succeed("  Hello, VibeLang!\n") }) { try greet() }
// Output: Hello, VibeLang!

greet depends only on FileSystem, so production entry points wire a real runtime layer while tests supply FileSystem.layerNoop.

Supplying Node or Deno implementations

// main-node.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import * as NodeServices from "@effect/platform-node/NodeServices"
import { greet } from "./greeting.vibe"
 
effect function main() {
  const fs = try FileSystem.FileSystem
  try fs.writeFileString("./greeting.txt", "Hello, VibeLang!")
  try greet()
}
 
NodeRuntime.runMain(Effect.provide(main(), NodeServices.layer))
// Output: Hello, VibeLang!

@effect/platform-node runs on both Node.js and Deno's Node compatibility layer (subject to Deno permissions). An inner provide overrides a single service inside its block.

Supplying Bun implementations

// main-bun.vibe
import * as BunRuntime from "@effect/platform-bun/BunRuntime"
import * as BunServices from "@effect/platform-bun/BunServices"
import { greet } from "./greeting.vibe"
 
effect function main() {
  const fs = try FileSystem.FileSystem
  try fs.writeFileString("./greeting.txt", "Hello, VibeLang!")
  try greet()
}
 
BunRuntime.runMain(Effect.provide(main(), BunServices.layer))
// Output: Hello, VibeLang!

Swap the entry module to BunRuntime and BunServices.layer; greet stays unchanged. Provide narrower layers (such as NodeFileSystem.layer) when a program only needs one service.

Starting main in a browser

Use BrowserRuntime.runMain from @effect/platform-browser (with BrowserCrypto.layer for crypto and ExperimentalBrowserServices.layer for Experimental.Locks or Experimental.MediaQuery). Keep the program separate from the page entry module:

// app.vibe
export effect function main() {
  const mac = try Mac.Mac
  const encoder = new TextEncoder()
  const key = try Mac.key(Mac.hmacSha256, encoder.encode("key"))
  const tag = try mac.sign(key, encoder.encode("hello"))
  try Console.log(tag.length)
}
// page.vibe
import { BrowserRuntime } from "@effect/platform-browser"
import * as BrowserCrypto from "@effect/platform-browser/BrowserCrypto"
import { main } from "./app.vibe"
 
BrowserRuntime.runMain(Effect.provide(main(), BrowserCrypto.layer))

Load the compiled page.js as a module script; BrowserRuntime.runMain interrupts main automatically when the page unloads.

Coming from Effect

An effect function returns an Effect. try replaces yield* for both services and operations, provide layer { ... } supplies a layer to a block, and the entry module runs main() with NodeRuntime, BunRuntime, or BrowserRuntime.


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