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 |
|---|---|---|
| Command | Provides a way to interact with the command line. | Stable |
| FileSystem | A module for file system operations. | Stable |
| KeyValueStore | Manages key-value pairs for data storage. | 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 |
Unstable Modules
Some modules in @effect/platform are still in development or marked as experimental.
These features are subject to change.
| Module | Description | Status |
|---|---|---|
| Http API | Provide a declarative way to define HTTP APIs. | Unstable |
| Http Client | A client for making HTTP requests. | Unstable |
| Http Server | A server for handling HTTP requests. | Unstable |
| Socket | A module for socket-based communication. | Unstable |
| Worker | A module for running tasks in separate workers. | Unstable |
For the most up-to-date documentation and details, please refer to the official README of the package.
Installation
To install the beta version:
npm install @effect/platformpnpm add @effect/platformyarn add @effect/platformbun add @effect/platformdeno add npm:@effect/platformGetting 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 { Path } from "@effect/platform"import { Effect } 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)})Running the Program in Node.js or Deno
First, install the Node.js-specific package:
npm install @effect/platform-nodepnpm add @effect/platform-nodeyarn add @effect/platform-nodedeno add npm:@effect/platform-nodeUpdate the program to load the Node.js-specific context:
Example (Providing Node.js Context)
import { Path } from "@effect/platform"import { Effect } from "effect"import { NodeContext, 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(NodeContext.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-bunUpdate the program to use the Bun-specific context:
Example (Providing Bun Context)
import { Path } from "@effect/platform"import { Effect } from "effect"import { BunContext, 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(BunContext.layer)))Run the program in Bun:
bun index.tstmp/file.txt