Skip to main content

Monorepo Setup

Alchemy works seamlessly in monorepo setups, allowing you to manage multiple applications and shared infrastructure in a single repository. This guide shows you how to structure and deploy a monorepo with Alchemy.

Monorepo Structure

A typical Alchemy monorepo looks like this:

Setting Up a Monorepo

Resource Sharing Between Apps

Apps can import and use resources from other apps:

Export Resources

apps/backend/alchemy.run.ts

Import and Use Resources

apps/frontend/alchemy.run.ts
Alchemy automatically handles resource dependencies and creates them in the correct order.

Deployment

Deploy All Apps

Deploy all applications in dependency order:
Turborepo ensures:
  • Dependencies are deployed first
  • Apps are deployed in parallel when possible
  • Failures are handled gracefully

Deploy Specific App

Deploy with Stage

Destroying Resources

Destroy All Apps

This destroys resources in reverse dependency order:
  1. Frontend (destroys last)
  2. Backend and Analytics (can destroy in parallel)

Destroy Specific App

Be careful when destroying apps with shared resources. Other apps may depend on them.

Environment Variables

Shared Environment Variables

Use a root .env file for shared configuration:
.env

App-Specific Environment Variables

Each app can have its own .env.local:
apps/backend/.env.local

Loading Environment Variables

Stages and Environments

Per-Stage Deployments

Deploy different stages of the monorepo:

Environment-Specific Configuration

apps/backend/alchemy.run.ts

State Management

Each application maintains its own state:
State is isolated per application and stage, allowing independent deployments.

Development Workflow

Local Development

Run all apps locally:

Watch Mode

Each app can run in watch mode independently:

Best Practices

Example: Complete Monorepo

Here’s a complete example monorepo:

Troubleshooting

Circular Dependencies

Avoid circular imports between apps:
Solution: Create a shared package for common resources.

Import Errors

Ensure package exports are configured:
apps/backend/package.json

Deployment Order

Use Turborepo’s dependsOn to control order:
turbo.json

Next Steps