Skip to content

Path

Path provides Effect's portable path manipulation service in place of node:path, avoiding hard-coded separators and native imports.

Requesting the service

import * as NodePath from "@effect/platform-node/NodePath"
 
effect function pathSeparator() {
  const path = try Path.Path
  return path.sep
}
// Run in an effect function: provide NodePath.layer { try Console.log(try pathSeparator()) }
// Output: /

try Path.Path fetches the active Path implementation and adds Path.Path to R. Module exports such as Path.layer retain their normal meaning.

Manipulating paths

import * as NodePath from "@effect/platform-node/NodePath"
 
effect function describeReport() {
  const path = try Path.Path
  const joined = path.join("reports", "weekly.txt")
  const normalized = path.normalize("reports/./draft/../weekly.txt")
  const parsed = path.parse(joined)
  return {
    joined,
    normalized,
    base: path.basename(joined),
    directory: path.dirname(joined),
    extension: path.extname(joined),
    absolute: path.isAbsolute(joined),
    rebuilt: path.format(parsed)
  }
}
// Run in an effect function: provide NodePath.layer { { const jsonValue = try describeReport(); try Console.log(try Effect.try(() => JSON.stringify(jsonValue))) } }
// Output: {"joined":"reports/weekly.txt","normalized":"reports/weekly.txt","base":"weekly.txt","directory":"reports","extension":".txt","absolute":false,"rebuilt":"reports/weekly.txt"}

Most Path operations are synchronous string functions—call them directly without try. Wrap any synchronous call that might throw in Effect.try.

OperationResult
basename, dirname, extnameFile name, parent directory, or extension
join, normalizeJoined or normalized path
parse, formatParsed path components or formatted string
isAbsoluteWhether the path has an absolute root
relative, resolveRelative path or resolved absolute path
sepHost path separator
toNamespacedPathWindows namespace path (no-op on POSIX)
toFileUrl, fromFileUrlEffectful file:// URL conversion
import * as NodePath from "@effect/platform-node/NodePath"
 
effect function asFileUrl(file: string) {
  const path = try Path.Path
  const absolute = path.resolve(file)
  const url = try path.toFileUrl(absolute)
  return try path.fromFileUrl(url)
}
// Run in an effect function: provide NodePath.layer { try Console.log(try asFileUrl("/reports/weekly.txt")) }
// Output: /reports/weekly.txt

toFileUrl and fromFileUrl return Effects (failing with BadArgument on invalid input). All Path operations are purely lexical and never touch the filesystem—use FileSystem to check existence or resolve symlinks.

Platform layers

// main.vibe
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import * as NodePath from "@effect/platform-node/NodePath"
 
effect function main() {
  const path = try Path.Path
  try Console.log(path.join("reports", "weekly.txt"))
}
 
NodeRuntime.runMain(Effect.provide(main(), NodePath.layer))
// Output: reports/weekly.txt

Provide NodePath.layer (or NodeServices.layer / BunServices.layer) for host conventions, or Path.layer for deterministic POSIX paths across platforms and tests.

Coming from Effect

Fetch the service with try Path.Path, read properties like path.sep and call synchronous helpers directly, and run toFileUrl / fromFileUrl with try.


The matching Effect page: effect.website/docs/v4/platform/path