Skip to content

FileSystem

FileSystem provides Effect's portable file operations in place of node:fs. Fetch the service with try FileSystem.FileSystem, execute methods with try, and wire a runtime or test layer at the entry point.

Requesting the service

effect function readSettings() {
  const fs = try FileSystem.FileSystem
  return try fs.readFileString("./settings.json")
}
// Run in an effect function: provide FileSystem.layerNoop({ readFileString: () => succeed('{"theme":"dark"}') }) { try Console.log(try readSettings()) }
// Output: {"theme":"dark"}

try FileSystem.FileSystem adds FileSystem.FileSystem to R; file operations add PlatformError to E. In type annotations, write FileSystem.FileSystem rather than bare FileSystem (which can collide with the DOM interface).

File operations

import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
 
effect function saveSummary(text: string) {
  const fs = try FileSystem.FileSystem
  try fs.makeDirectory("./output", { recursive: true })
  try fs.writeFileString("./output/summary.txt", text)
  const content = try fs.readFileString("./output/summary.txt", "utf8")
  return { content, entries: try fs.readDirectory("./output") }
}
// Run in an effect function: provide NodeFileSystem.layer { { const jsonValue = try saveSummary("ready"); try Console.log(try Effect.try(() => JSON.stringify(jsonValue))) } }
// Output: {"content":"ready","entries":["summary.txt"]}

Operations execute sequentially in statement order; handle expected PlatformError values with catch. Use readFile / writeFile for raw Uint8Array bytes.

OperationsPurpose
access, existsCheck permissions or existence
copy, copyFileCopy a directory tree or file
chmod, chownUpdate permissions or ownership
link, symlink, readLinkManage hard and symbolic links
makeDirectory, readDirectoryCreate or list directories
makeTempDirectory, makeTempFileCreate caller-owned temporary paths
makeTempDirectoryScoped, makeTempFileScopedCreate temporary paths removed on scope exit
openOpen a scope-managed file handle
readFile, readFileStringRead bytes or UTF-8 text
writeFile, writeFileStringWrite bytes or UTF-8 text
stat, realPathRead metadata or resolve symlinks
remove, rename, truncate, utimesDelete, move, resize, or update timestamps
stream, sink, watchStream bytes, write via sink, or watch paths
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
 
effect function fileSize(path: string) {
  return try scoped(effect {
    const fs = try FileSystem.FileSystem
    const file = try fs.open(path)
    return (try file.stat).size
  })
}
// Run in an effect function: provide NodeFileSystem.layer { try scoped(effect { const fs = try FileSystem.FileSystem; const path = try fs.makeTempFileScoped(); try fs.writeFileString(path, "hello"); try Console.log(String(try fileSize(path))) }) }
// Output: 5

open and the *Scoped constructors require Scope and register cleanup automatically. Wrap them in scoped(effect { ... }) to close handles on normal exit, failure, or interruption; omit scoped to bind them to the caller's scope.

Platform layers

// main.vibe
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
 
effect function main() {
  provide NodeFileSystem.layer {
    try scoped(effect {
      const fs = try FileSystem.FileSystem
      const path = try fs.makeTempFileScoped()
      try fs.writeFileString(path, "Project notes")
      try Console.log(try fs.readFileString(path))
    })
  }
}
 
NodeRuntime.runMain(main())
// Output: Project notes

Provide NodeFileSystem.layer for standalone file access, or NodeServices.layer / BunServices.layer for the full host bundle.

Testing with FileSystem.layerNoop

// files.vibe
export effect function readFixture() {
  const fs = try FileSystem.FileSystem
  if (!(try fs.exists("/fixtures/title.txt"))) return "Untitled"
  return try fs.readFileString("/fixtures/title.txt")
}
 
export const FilesTest = FileSystem.layerNoop({
  exists: (path) => succeed(path === "/fixtures/title.txt"),
  readFileString: () => succeed("Release notes")
})
// Run in an effect function: provide FilesTest { try Console.log(try readFixture()) }
// Output: Release notes
// files.test.vibe
import { assert, it } from "@effect/vitest"
import { FilesTest, readFixture } from "./files.vibe"
 
it.effect("reads the configured title", () => effect {
  provide FilesTest {
    const title = try readFixture()
    try Effect.try(() => assert.strictEqual(title, "Release notes"))
  }
})

FileSystem.layerNoop stubs only the methods you pass; unimplemented methods fail or die rather than simulating an in-memory filesystem, exposing unexpected I/O in tests.

Coming from Effect

Overrides in layerNoop return explicit Effects: () => succeed("Release notes") for constants, or effect () => { ... } for multi-step stubs.


The matching Effect page: effect.website/docs/v4/platform/file-system