Desktop Apps
The same Farm.js app builds for the web or the desktop. With --desktop, the Farm.js production server
runs inside a desktop shell on 127.0.0.1, and a native window shows it. Pages, server functions,
API routes, middleware, and local files work as they do on a server.
farm dev --desktop # the app in its window, with HMR
farm build --desktop # a packaged app
farm build # --web, the default, unchangedTo ship the same source as a native mobile app, see Mobile Apps.
Setup
Add desktop to the config and install @farm.js/desktop with the shell's toolkit:
import { defineConfig } from "@farm.js/core";
export default defineConfig({
desktop: {
name: "Notes",
id: "com.example.notes",
window: { width: 1100, height: 760 },
},
});pnpm add -D @farm.js/desktop electrobun| Option | Default | Notes |
|---|---|---|
name | package.json productName or name | Display name |
id | local.farm.<name> | Reverse-DNS identifier. The OS keys the app's data directory on it |
version | package.json version | |
icon | the Farm.js icon | Square PNG relative to the app root; 1024×1024 recommended |
window.width | 1024 | Points |
window.height | 768 | Points |
window.title | name | |
window.hideOnClose | false | Closing the window hides it and the app keeps running (see below) |
permissions.<name> | none | macOS only: the sentence shown when the app asks (see below) |
shell | "electrobun" | Resolves to @farm.js/desktop/<shell>, or a package name |
A web build checks a static block's shape and otherwise ignores it: Farm.js removes desktop from
the config module a server bundle imports, so the output is byte-identical to the same app without
the key. --desktop fails with an actionable message when the key or the shell package is missing.
Function-valued blocks
desktop can also be a function, sync or async, that returns the block:
export default defineConfig({
desktop: async ({ os, mode, command, profile, env, root }) => ({
name: profile === "beta" ? "Notes Beta" : "Notes",
id: profile === "beta" ? "com.example.notes.beta" : "com.example.notes",
window: { width: os === "macos" ? 1100 : 1200, height: 760 },
}),
});| Field | Value |
|---|---|
os | "macos", "windows", or "linux" (the host) |
mode | "development" or "production" |
command | "dev" or "build" |
profile | --profile <name>, "production" by default |
env | a frozen snapshot of process.env |
root | the app root |
The function runs once when farm dev --desktop starts and once per farm build --desktop. A web
build never calls it. Only the values it returns reach the generated shell; the function and
anything it imports never ship, so it may read files, call the network, or use env. The returned
block is validated with errors that name the OS and field, such as
desktop (os: windows): window.width must be a whole number of points between 100 and 16384.
farm build --desktop --profile beta
farm config inspect --desktop --os windows --profile beta # prints the resolved blockPlatform and app data
import { join } from "node:path";
import { appDataDir, platform } from "@farm.js/core/platform";
if (platform() === "desktop") {
const file = join(appDataDir(), "notes.json");
}platform() returns "web" or "desktop". In browser code it is replaced with a literal at build
time, so platform() === "desktop" branches fold away.
| Mode | appDataDir() |
|---|---|
| packaged desktop app | the OS user data directory for desktop.id, created on use |
farm dev --desktop | .farm/data/desktop |
| web | FARM_DATA_DIR when set, otherwise it throws |
appDataDir() is server-only. Never write next to the app's code: a packaged app is read-only and
signed.
Permissions
macOS lets the app use the microphone, camera, location and other private data only when the app
declares it, with a sentence that says why. Declare each one under permissions; the value is that
sentence:
export default defineConfig({
desktop: {
name: "Notes",
id: "com.example.notes",
permissions: {
microphone: "Notes records voice memos.",
camera: "Notes scans whiteboards into a note.",
location: "Notes tags a note with where you wrote it.",
},
},
});The page then uses the web APIs as it would in a browser:
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const recorder = new MediaRecorder(stream);
recorder.start();| Name | For | Info.plist key |
|---|---|---|
microphone | getUserMedia({ audio: true }) | NSMicrophoneUsageDescription |
camera | getUserMedia({ video: true }) | NSCameraUsageDescription |
location | navigator.geolocation | NSLocationUsageDescription, NSLocationWhenInUseUsageDescription |
speechRecognition | WebKit's SpeechRecognition (with microphone) | NSSpeechRecognitionUsageDescription |
bluetooth | native code in the main process | NSBluetoothAlwaysUsageDescription |
contacts | native code | NSContactsUsageDescription |
calendars | native code | NSCalendarsUsageDescription |
reminders | native code | NSRemindersUsageDescription |
photos | native code | NSPhotoLibraryUsageDescription |
music | native code | NSAppleMusicUsageDescription |
motion | native code | NSMotionUsageDescription |
documents | files in Documents | NSDocumentsFolderUsageDescription |
downloads | files in Downloads | NSDownloadsFolderUsageDescription |
desktop | files on the Desktop | NSDesktopFolderUsageDescription |
Without a permission, macOS refuses it without asking: getUserMedia({ audio: true }) fails with no
microphone, navigator.geolocation with no location. Declaring one doesn't prompt anyone; macOS
asks the first time the app uses it. Each value must be a non-empty string. The sentences go into
the app's Info.plist when the shell is built, so a change applies on the next farm dev --desktop
or farm build --desktop. Windows and Linux builds ignore them.
Screen recording (getDisplayMedia) and notifications have no Info.plist key: macOS asks for them
on first use without one. Apple Events (NSAppleEventsUsageDescription) and local network access
(NSLocalNetworkUsageDescription) can't be declared: Electrobun 2.0.5 has no way to write them, so
permissions rejects them as unknown.
The local guard
Any local process and any page in the user's browser can reach 127.0.0.1, so desktop builds and
farm dev --desktop answer only their own window:
- The shell starts the server with a per-launch secret and a one-time handshake token, then opens
the window at
/__farm/desktop/handshake?t=<token>. The handshake consumes the token, sets the secret as anHttpOnly,SameSite=Strictcookie namedfarm_desktop, and redirects to/. - Every request needs that cookie, compared in constant time: pages, API routes, server functions, plugin endpoints, Farm.js's built-in endpoints, client assets, and WebSocket upgrades. In dev it also covers Vite's module requests.
Hostmust be127.0.0.1,localhost, or[::1]on the server's port, which blocks DNS rebinding. Requests that Fetch Metadata markscross-siteorsame-site(a page on another localhost port) are rejected, and unsafe methods with anOriginmust come from the server's own origin.
The guard runs before plugins, middleware, and routing, and is not configurable. Web builds do not contain it. It protects the HTTP surface, not the disk: code running as the same OS user can read the app's data files directly.
Desktop defaults
A desktop app is not a public site. Unless the config sets them, desktop builds turn off:
agent.sitemap,agent.llmsTxt,agent.manifest,agent.aiCatalog, and the default/mcpserver (agent.mcp): no agent could reach them through the guard;agent.jsonLd;- image optimization (
images.providerbecomes"none", which keeps sharp's native binaries out of the app).
Output
.farm/output/desktop/
server/ Farm.js server for the shell's runtime; server/index.mjs exports start()
shell/ generated shell main process and config (regenerated on every build)
dist/ the packaged appThe server is the bun preset, or node-server for shells that embed Node. HOST and PORT in
the environment do not apply: the server binds 127.0.0.1 on a port the OS assigns.
Electrobun
@farm.js/desktop/electrobun is the default shell. It generates electrobun.config.ts and the
main process into .farm/output/desktop/shell on every run, embeds the Farm.js server in Electrobun's
Bun, and uses the system WebView (WKWebView on macOS), so no Chromium ships with the app.
farm dev --desktoprunselectrobun devwith the window onfarm dev.farm build --desktoprunselectrobun build --env=stableand moves the app to.farm/output/desktop/dist, with update archives indist/update.appDataDir()is Electrobun'sUtils.paths.userData, for example~/Library/Application Support/<desktop.id>/stableon macOS.- Closing the window quits the app. With
window.hideOnClose: trueit hides the window instead and the app keeps running: clicking the Dock icon on macOS shows it again, and so canwindow.show()fromsrc/desktop.ts(a menu-bar item's "Open", say). On Windows and Linux, only the app's own code can bring the window back, so give it a way to. - Quitting the app drains requests and runs Farm.js's shutdown hooks before the process exits.
- The window is sandboxed: page scripts and frames get no Electrobun RPC bridge.
desktop.icon(or the Farm.js icon) becomes the app'sAppIcon.icnson macOS.desktop.permissionsbecomesbuild.mac.entitlements(com.apple.security.device.audio-inputformicrophone,com.apple.security.device.cameraforcamera, and so on), which Electrobun writes into Info.plist. When the page asks for a device, the user sees Electrobun's Allow/Block dialog and, the first time, macOS's prompt with the app's sentence.- Known limit: the first launch after an install or update leaves Electrobun's hidden unpacking process running until logout. Later launches don't.
The first electrobun command downloads Hutch and the Electrobun runtime into ~/.hutch. The
packaged app ships its own Bun. Measured on macOS 26, Apple silicon, with
examples/desktop-electrobun: a 32 MB .app (94 MB after the first launch unpacks it, 85 MB of it
Bun) and a 3.1 MB server.
Optional hooks run in the shell process:
// src/desktop.ts
import { defineDesktop } from "@farm.js/core/desktop";
export default defineDesktop({
onReady({ window, server, mode }) {
console.log(`Notes on ${server.origin} (${mode})`);
},
});Shell adapters
@farm.js/desktop ships the built-in shells as subpaths (@farm.js/desktop/electrobun). Any other
shell is a package, or a package subpath, whose default export implements DesktopShell from
@farm.js/core/desktop; set desktop.shell to its name:
import { defineDesktopShell } from "@farm.js/core/desktop";
export default defineDesktopShell({
name: "my-shell",
runtime: "bun",
generateMain(context) {
return [{ path: "main.ts", contents: "..." }];
},
async dev(context) {
// Open a window on context.devUrl at the handshake URL.
return { exited, close };
},
async package(context) {
return { files: [] };
},
});A Bun or Node shell imports the server in-process:
const { start } = await import("./server/server/index.mjs");
const server = await start({ secret, handshakeToken, dataDir });
// server.origin, server.port; await server.stop() drains requests and runs shutdown hooks.A shell that runs the server as its own process sets FARM_DESKTOP_SECRET,
FARM_DESKTOP_HANDSHAKE_TOKEN, and FARM_DESKTOP_DATA_DIR, runs server/index.mjs, and waits for
the line farm:ready {"port":N} on stdout.
Build a native desktop app from the same Farm.js source with farm dev --desktop and farm build --desktop.