Multiple Apps, Turborepo
In this guide, we’ll walk through how to:
- Set up a simple monorepo with two applications: a
backend
and afrontend
- Import and bind to the
backend
application from thefrontend
application - Deploy, destroy, and dev all applications in the monorepo.
-
Set up the
deploy
anddev
tasks to run in dependency order (backend → frontend
):turbo.json {"$schema": "https://turborepo.com/schema.json","ui": "tui","tasks": {"deploy": {"dependsOn": ["^deploy"],"cache": false},"dev": {"persistent": true,"cache": false},}} -
Set up the
destroy
task to run in reverse dependency order (frontend → backend
):turbo.json {"$schema": "https://turborepo.com/schema.json","ui": "tui","tasks": {..."backend#destroy": {"dependsOn": ["frontend#destroy"],"cache": false},"frontend#destroy": {"cache": false}}} -
Update the
backend
app to useimport.meta.dirname
to locate theentrypoint
so that it can be run from any directory (e.g. imported fromapps/frontend
).apps/backend/alchemy.run.ts import alchemy from "alchemy";import { D1Database, Worker } from "alchemy/cloudflare";import path from "node:path";const app = await alchemy("backend");const db = await D1Database("db");export const backend = await Worker("worker", {entrypoint: path.join("src", "worker.ts"),entrypoint: path.join(import.meta.dirname, "src", "worker.ts"),bindings: {db,API_KEY: alchemy.secret.env.API_KEY,},});console.log({ url: backend.url });await app.finalize(); -
Install
alchemy
and configuredev
,deploy
anddestroy
in thebackend
app.apps/backend/package.json {"name": "backend","private": true,"type": "module","scripts": {"build": "tsc -b","dev": "alchemy dev --app backend","deploy": "alchemy deploy --app backend","destroy": "alchemy destroy --app backend"},"dependencies": {"alchemy": "catalog:"}} -
Export
backend/alchemy.run.ts
as./alchemy
.apps/backend/package.json "exports": {".": {"bun": "./src/index.ts","import": "./lib/src/index.js",},"./alchemy": {"bun": "./alchemy.run.ts","import": "./lib/alchemy.run.js""types": "./alchemy.run.ts"}}, -
Add the
backend
service as aworkspace:*
dependency in thefrontend
app.apps/frontend/package.json {"name": "frontend","private": true,"type": "module","dependencies": {"backend": "workspace:*","alchemy": "catalog:"}} -
Import
backend/alchemy
and bind to the exportedbackend
Worker:apps/frontend/alchemy.run.ts import alchemy from "alchemy";import { Vite } from "alchemy/cloudflare";import { backend } from "backend/alchemy";const app = await alchemy("frontend");export const frontend = await Vite("website", {bindings: {backend,},});console.log({url: frontend.url,});await app.finalize(); -
Use
--app frontend
in your frontend’sdev
,deploy
anddestroy
scripts:apps/frontend/package.json {"name": "frontend","private": true,"type": "module","scripts": {"build": "tsc -b","dev": "alchemy dev --app frontend","deploy": "alchemy deploy --app frontend","destroy": "alchemy destroy --app frontend"},"dependencies": {"backend": "workspace:*","alchemy": "catalog:"}} -
Run
bun dev
from the root to start the development server for thefrontend
app.Terminal window bun devTerminal window npm run devTerminal window yarn devTerminal window pnpm devYou should see a turborepo TUI with a pane for each app.
-
When ready, run
bun deploy
from the root to deploy thefrontend
app.Terminal window bun run deployTerminal window npm run deployTerminal window yarn deployTerminal window pnpm run deploy -
(Optional) Tear down both apps:
Terminal window bun run destroyTerminal window npm run destroyTerminal window yarn run destroyTerminal window pnpm run destroy