Skip to content
GitHubXDiscord

File

The File resource lets you create, update and delete files in the filesystem with automatic directory creation and cleanup.

Create a simple text file:

import { File } from "alchemy/fs";
const config = await File("config.txt", {
path: "config.txt",
content: "some configuration data",
});

The File resource will automatically create parent directories as needed:

import { File } from "alchemy/fs";
const log = await File("logs/app.log", {
path: "logs/app.log",
content: "application log entry",
});

When updating a file’s path, the old file is automatically removed:

import { File } from "alchemy/fs";
let file = await File("config.json", {
path: "config.json",
content: '{ "version": "1.0.0" }',
});
// Later, update path and content (old file will be removed)
file = await File("config.json", {
path: "config/config.json",
content: '{ "version": "1.0.1" }',
});

The fs service provides specialized file types for common formats:

import {
StaticJsonFile,
StaticTypeScriptFile,
StaticYamlFile,
} from "alchemy/fs";
// Create formatted JSON file
const config = await StaticJsonFile("config.json", {
api: { endpoint: "https://api.example.com" },
});
// Create formatted TypeScript file
const component = await StaticTypeScriptFile(
"Component.ts",
`
export function Component() {
return <div>Hello</div>
}
`
);
// Create YAML file
const deployment = await StaticYamlFile("deploy.yaml", {
service: { replicas: 3 },
});