Skip to content
Helix Docs

ctx.fs()

Reference for ctx.fs(), the project's own directory of files: root, resolve(), the Node fs/promises methods, and the errors it throws.

For developers Updated Nov 23, 2026
View as Markdown

ctx.fs() returns a handle to the project’s own directory of ordinary files, with the familiar fs/promises methods. Call it with no arguments. The directory persists across executions and deployments, and every function in the project sees the same one. It is turned on per organization, and your project has to call ctx.fs() in its own source for Helix to provide it.

Signature

fs(): ProjectFileSystem

Calling ctx.fs(), reading root, and calling resolve() never touch the disk. The first file operation does. Setup, local development, and the constraints live in the project files guide.

ProjectFileSystem

Every path argument is relative to root. Arguments and return types match Node’s fs/promises, and Node’s errors reach you unchanged, so err.code === 'ENOENT' works as usual.

MemberReturnsNotes
rootstringAbsolute path of the project’s directory. It differs between helix dev and a deployed run, so don’t hard-code it.
resolve(...segments)stringJoins the segments under root and returns the absolute path, for libraries that take a path. It checks the path as text and doesn’t look at symlinks or the disk.
readFile(path, options?)Promise<Buffer> or Promise<string>A string when you pass an encoding.
writeFile(path, data, options?)Promise<void>Replaces the file.
appendFile(path, data, options?)Promise<void>Creates the file if it’s missing.
readdir(path, options?)Promise<string[]> or Promise<Dirent[]>Dirent entries with withFileTypes: true. Accepts recursive.
stat(path, options?)Promise<Stats>BigIntStats with bigint: true.
lstat(path, options?)Promise<Stats>Describes a symlink instead of following it.
mkdir(path, options?)Promise<void>, or Promise<string | undefined> with recursive: trueWith recursive: true, resolves to the first directory created, or undefined when it already existed.
rm(path, options?)Promise<void>Accepts recursive and force. The project’s directory itself can’t be removed.
rename(from, to)Promise<void>Both paths must stay inside the directory. The directory itself can’t be renamed.
copyFile(from, to, mode?)Promise<void>Both paths must stay inside the directory.
access(path, mode?)Promise<void>Rejects when the path is missing.
truncate(path, length?)Promise<void>Resizes a file.
createReadStream(path, options?)ReadStreamThe fd and fs options are rejected.
createWriteStream(path, options?)WriteStreamThe fd and fs options are rejected.

Paths stay inside the directory

A path that would leave the project’s directory is rejected before anything runs:

  • A .. segment, even one that would stay inside, such as a/../b.
  • An absolute path.
  • A NUL byte.
  • A symlink that points outside the directory, in any part of the path.

resolve() rejects the first three. Every file operation also follows symlinks and rejects one that leads out, so a link created by a library can’t be used to reach other files.

Errors

Read err.code on the thrown error.

CodeHTTP status when uncaughtWhen
FS_PATH_INVALID400A path leaves the project’s directory. The message says why.
FS_NOT_MOUNTED500The deployed function has no project file system. The message says to redeploy. See when something fails.
FS_NOT_AVAILABLE501The environment has no project directory to give.

The caller of an uncaught FS_NOT_MOUNTED sees only a generic server error. The cause is in the execution’s logs. Operating system errors such as ENOENT and EEXIST pass through with their code.

Example

// functions/api/v1/notes.post.ts
import { z } from 'zod';
import { defineFunction } from '@trayai/helix-sdk';

export const input = z.object({
  name: z.string().regex(/^[\w-]+$/),
  text: z.string(),
});

export default defineFunction<typeof input>(async (ctx) => {
  const fs = ctx.fs();

  await fs.mkdir('notes', { recursive: true });
  await fs.writeFile(`notes/${ctx.input.name}.txt`, ctx.input.text);

  return { saved: ctx.input.name, notes: await fs.readdir('notes') };
});

The pattern on name keeps a caller from sending ../x, which ctx.fs() would reject with FS_PATH_INVALID anyway.

Loading search…

Jump to a section

tab to move · esc to close