Introduction
On this page
Welcome to the documentation for @effect/platform
, a library designed for creating platform-independent abstractions (Node.js, Bun, browsers).
With @effect/platform
, you can incorporate abstract services like Terminal
, FileSystem
or Path
into your program.
Later, during the assembly of the final application, you can provide specific layers for the target platform using the corresponding packages:
@effect/platform-node
for Node.js@effect/platform-bun
for Bun@effect/platform-browser
for browsers
Installation
To install the beta version:
bash
npm install @effect/platform
bash
npm install @effect/platform
Example: Platform-Agnostic Program
Below is a simple example using the Path
module to create a file path. This program is compatible with multiple environments:
ts
import {Path } from "@effect/platform"import {Effect } from "effect"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =path .join ("tmp", "file.txt")console .log (mypath )})
ts
import {Path } from "@effect/platform"import {Effect } from "effect"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =path .join ("tmp", "file.txt")console .log (mypath )})
Running the Program in Node.js
First, install the Node.js-specific package:
bash
npm install @effect/platform-node
bash
npm install @effect/platform-node
Update the program to load the Node.js-specific context:
ts
import {Path } from "@effect/platform"import {Effect } from "effect"import {NodeContext ,NodeRuntime } from "@effect/platform-node"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =path .join ("tmp", "file.txt")console .log (mypath )})NodeRuntime .runMain (program .pipe (Effect .provide (NodeContext .layer )))
ts
import {Path } from "@effect/platform"import {Effect } from "effect"import {NodeContext ,NodeRuntime } from "@effect/platform-node"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =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
:
bash
tsx index.ts# Output: tmp/file.txt
bash
tsx index.ts# Output: tmp/file.txt
Running the Program in Bun
To run the same program in Bun, first install the Bun-specific package:
bash
npm install @effect/platform-bun
bash
npm install @effect/platform-bun
Update the program to use the Bun-specific context:
ts
import {Path } from "@effect/platform"import {Effect } from "effect"import {BunContext ,BunRuntime } from "@effect/platform-bun"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =path .join ("tmp", "file.txt")console .log (mypath )})BunRuntime .runMain (program .pipe (Effect .provide (BunContext .layer )))
ts
import {Path } from "@effect/platform"import {Effect } from "effect"import {BunContext ,BunRuntime } from "@effect/platform-bun"constprogram =Effect .gen (function* () {constpath = yield*Path .Path constmypath =path .join ("tmp", "file.txt")console .log (mypath )})BunRuntime .runMain (program .pipe (Effect .provide (BunContext .layer )))
Run the program in Bun:
bash
bun index.ts# Output: tmp/file.txt
bash
bun index.ts# Output: tmp/file.txt