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().
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 processtry 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:
| Call | Effect | Layer |
|---|---|---|
fs.readFile | try (try FileSystem.FileSystem).readFile(path) | NodeServices.layer |
os.tmpdir | try (try Experimental.Process.Process).temporaryDirectory | ExperimentalNodeServices.layer |
net.isIPv6 | Experimental.NetAddress.isIPv6(address) | none (pure) |
child_process.spawn with inherited stdio | try (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.execFileSync | try (try ChildProcessSpawner.ChildProcessSpawner).string(ChildProcess.make(command, args)) | NodeServices.layer |
fetch | try HttpClient.get(url) | FetchHttpClient.layer |
http.createServer | try Layer.launch(HttpRouter.serve(app)) | ExperimentalNodeHttpServer.layer({ port }) |
crypto.randomUUID | try (try SecureRandom.SecureRandom).uuidV4() | NodeCrypto.layer |
crypto.subtle.digest | try (try Digest.Digest).digest(Digest.sha256, bytes) | NodeCrypto.layer |
process.argv.slice(2) | try (try Stdio.Stdio).args | NodeServices.layer |
process.getuid?.() | try (try Experimental.Process.Process).currentUid | ExperimentalNodeServices.layer |
os.hostname | try (try Experimental.Process.Process).hostname | ExperimentalNodeServices.layer |
os.homedir | try (try Experimental.Process.Process).homeDirectory | ExperimentalNodeServices.layer |
process.kill | try (try Experimental.Process.Process).kill(pid, { killSignal }) | ExperimentalNodeServices.layer |
fs.constants.X_OK | try (try Experimental.FileSystem.FileSystem).access(path, { executable: true }) | ExperimentalNodeServices.layer |
crypto.createHmac | try (try Mac.Mac).sign(key, data) | NodeCrypto.layer |
navigator.locks | try (try Experimental.Locks.Locks).hold(name) | ExperimentalBrowserLocks.layer |
process.env | try Config.String("NAME") | none |
console.log | try Console.log(message) | none |
setTimeout | try sleep("1 second") | none |
Date.now | try Clock.currentTimeMillis | none |
performance.now() - start | Number((try Clock.monotonicTimeNanos) - startNanos) / 1e6 | none |
Math.random | try Random.next | none |
Key notes:
- Bytes vs. UTF-8:
readFile(path)returns bytes; usereadFileString(path)for UTF-8 text. - Arguments:
Stdio.argsmatchesprocess.argv.slice(2). Use a.tsadapter if you need the executable or script path. - Monotonic time: capture
const startNanos = try Clock.monotonicTimeNanosbefore the work and subtract it from a later reading of the same clock. This clock has an arbitrary origin, which can differ fromperformance.now()on Node; keep nativeperformance.now()reads in a.tsadapter when you rely onperformance.timeOrigin. - Bundles:
NodeServices.layer(andBunServices.layer) providesFileSystem,Path,Stdio,Terminal, andChildProcessSpawner. Effect 4.1 crypto services (SecureRandom,Digest,Mac,Signature) come fromNodeCrypto.layer,BunCrypto.layer, orBrowserCrypto.layer. - Experimental services: VibeLang adds services Effect does not ship yet under
Experimental.*(Experimental platform APIs), backed byExperimentalNodeServices.layer,-bun, or-browserfromvibelang-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