Path

On this page

The @effect/platform/Path module provides a set of operations for working with file paths.

Basic Usage

The module provides a single Path tag, which acts as the gateway for interacting with paths.

ts
import { Path } from "@effect/platform"
import { Effect } from "effect"
 
const program = Effect.gen(function* () {
const path = yield* Path.Path
 
// use `path` to operate on paths
})
ts
import { Path } from "@effect/platform"
import { Effect } from "effect"
 
const program = Effect.gen(function* () {
const path = yield* Path.Path
 
// use `path` to operate on paths
})

The Path interface includes the following operations:

OperationDescription
basenameReturns the last part of a path, optionally removing a given suffix.
dirnameReturns the directory part of a path.
extnameReturns the file extension from a path.
formatFormats a path object into a path string.
fromFileUrlConverts a file URL to a path.
isAbsoluteChecks if a path is absolute.
joinJoins multiple path segments into one.
normalizeNormalizes a path by resolving . and .. segments.
parseParses a path string into an object with its segments.
relativeComputes the relative path from one path to another.
resolveResolves a sequence of paths to an absolute path.
sepReturns the platform-specific path segment separator (e.g., / on POSIX).
toFileUrlConverts a path to a file URL.
toNamespacedPathConverts a path to a namespaced path (specific to Windows).

Example: using join

ts
import { Path } from "@effect/platform"
import { Effect } from "effect"
import { NodeContext, NodeRuntime } from "@effect/platform-node"
 
const program = Effect.gen(function* () {
const path = yield* Path.Path
 
const mypath = 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"
 
const program = Effect.gen(function* () {
const path = yield* Path.Path
 
const mypath = path.join("tmp", "file.txt")
console.log(mypath)
})
 
NodeRuntime.runMain(program.pipe(Effect.provide(NodeContext.layer)))