Introduction to Effect Platform
@effect/platform is a library for building platform-independent abstractions in environments such as Node.js, Deno, Bun, and browsers.
With @effect/platform, you can integrate abstract services like FileSystem or Terminal into your program.
When assembling your final application, you can provide specific layers for the target platform using the corresponding packages:
@effect/platform-nodefor Node.js or Deno@effect/platform-bunfor Bun@effect/platform-browserfor browsers
Stable Modules
The following modules are stable and their documentation is available on this website:
| Module | Description | Status |
|---|---|---|
| FileSystem | A module for file system operations. | Stable |
| Path | Utilities for working with file paths. | Stable |
| PlatformLogger | Log messages to a file using the FileSystem APIs. | Stable |
| Runtime | Run your program with built-in error handling and logging. | Stable |
| Terminal | Tools for terminal interaction. | Stable |
Installation
To install the beta version:
npm install @effect/platform@rcpnpm add @effect/platform@rcyarn add @effect/platform@rcbun add @effect/platform@rcdeno add npm:@effect/platform@rcGetting Started with Cross-Platform Programming
Here’s a basic example using the Path module to create a file path, which can run across different environments:
Example (Cross-Platform Path Handling)
import { Effect, Path } from "effect"
const program = Effect.gen(function* () { // Access the Path service const path = yield* Path.Path
// Join parts of a path to create a complete file path const mypath = path.join("tmp", "file.txt")
console.log(mypath) return mypath})
Effect.runSync(program.pipe(Effect.provide(Path.layer))) // => "tmp/file.txt"Running the Program in Node.js or Deno
First, install the Node.js-specific package:
npm install @effect/platform-node@rcpnpm add @effect/platform-node@rcyarn add @effect/platform-node@rcdeno add npm:@effect/platform-node@rcUpdate the program to load the Node.js-specific context:
Example (Providing Node.js Context)
import { Effect, Path } from "effect"import { NodeServices, NodeRuntime } from "@effect/platform-node"
const program = Effect.gen(function* () { // Access the Path service const path = yield* Path.Path
// Join parts of a path to create a complete file path const mypath = path.join("tmp", "file.txt")
console.log(mypath)})
NodeRuntime.runMain(program.pipe(Effect.provide(NodeServices.layer)))Finally, run the program in Node.js using tsx, or directly in Deno:
npx tsx index.tspnpm dlx tsx index.tsyarn dlx tsx index.tsdeno run index.ts# or
deno run -RE index.ts# Output: tmp/file.txt# (granting required Read and Environment permissions without being prompted)Running the Program in Bun
To run the same program in Bun, first install the Bun-specific package:
bun add @effect/platform-bun@rcUpdate the program to use the Bun-specific context:
Example (Providing Bun Context)
import { Effect, Path } from "effect"import { BunServices, BunRuntime } from "@effect/platform-bun"
const program = Effect.gen(function* () { // Access the Path service const path = yield* Path.Path
// Join parts of a path to create a complete file path const mypath = path.join("tmp", "file.txt")
console.log(mypath)})
BunRuntime.runMain(program.pipe(Effect.provide(BunServices.layer)))Run the program in Bun:
bun index.tstmp/file.txt