node:fs API Reference

Filesystem APIs, streams, promises, and sandbox controls

Elide 1.5.0 exposes node:fs, node:fs/promises, fs.promises, and the elide:fs alias. The examples below cover synchronous, Promise, FileHandle, and stream operations.

Basic use

import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const dir = fs.mkdtempSync(path.join(os.tmpdir(), "elide-fs-"));
const file = path.join(dir, "hello.txt");

try {
  fs.writeFileSync(file, "hello\n");
  console.log(fs.readFileSync(file, "utf8"));
  console.log(fs.statSync(file).isFile());
} finally {
  fs.rmSync(dir, { recursive: true, force: true });
}

This example verifies mkdtempSync, writeFileSync, readFileSync, statSync, and rmSync. Filesystem failures may carry code, errno, syscall, path, and dest fields.

Promises and file handles

import os from "node:os";
import path from "node:path";
import { mkdtemp, open, rm, writeFile } from "node:fs/promises";

const dir = await mkdtemp(path.join(os.tmpdir(), "elide-fs-"));
try {
  const file = path.join(dir, "data.txt");
  await writeFile(file, "hello\n");

  const handle = await open(file, "r");
  try {
    const buffer = Buffer.alloc(64);
    const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
    console.log(buffer.subarray(0, bytesRead).toString());
  } finally {
    await handle.close();
  }
} finally {
  await rm(dir, { recursive: true, force: true });
}

This example verifies Promise-based mkdtemp, writeFile, open, and rm, plus FileHandle.read() and FileHandle.close(). Representative Promise failures expose the same Node-style fields described above.

Streams

createReadStream and createWriteStream return Readable and Writable streams. The following verified example copies a temporary file:

import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { pipeline } from "node:stream/promises";

const dir = fs.mkdtempSync(path.join(os.tmpdir(), "elide-fs-"));
try {
  const input = path.join(dir, "input.log");
  const output = path.join(dir, "copy.log");
  fs.writeFileSync(input, "first\nsecond\n");

  await pipeline(fs.createReadStream(input), fs.createWriteStream(output));
  console.log(fs.readFileSync(output, "utf8"));
} finally {
  fs.rmSync(dir, { recursive: true, force: true });
}

Watching limitation

watch, watchFile, unwatchFile, and the Promise watch surface are present. In Elide 1.5.0, a watcher does not keep a one-shot script alive or deliver a simple create event before the process exits. Do not rely on filesystem watching in Elide 1.5.0.

fs.openAsBlob is not available.

Sandbox

Filesystem access is unrestricted until sandbox flags are supplied:

  • --sandbox
  • --allow-read[=PATHS]
  • --allow-write[=PATHS]
  • --fs-audit

With a sandbox active, denied operations fail instead of accessing the path. See elide help sandbox for flag precedence and path-list syntax.

See the Node.js fs documentation for standard argument forms.