|
| 1 | +--- |
| 2 | +order: 7 |
| 3 | +title: Development Mode |
| 4 | +description: Learn how to use Alchemy's development mode to run you
F438
r application locally. |
| 5 | +--- |
| 6 | + |
| 7 | +# Development Mode (Beta) |
| 8 | + |
| 9 | +Alchemy's development mode provides a powerful local development experience for Cloudflare Workers, featuring hot reloading, local resource emulation, and seamless integration with remote Cloudflare services. |
| 10 | + |
| 11 | +> **Note:** Development mode is currently in beta. Some features may not work as expected. |
| 12 | +
|
| 13 | +## Overview |
| 14 | + |
| 15 | +To run Alchemy in development mode, use the `--dev` flag when running your `alchemy.run.ts` script: |
| 16 | + |
| 17 | +```bash |
| 18 | +bun run alchemy.run.ts --dev |
| 19 | +npx tsx alchemy.run.ts --dev |
| 20 | +``` |
| 21 | + |
| 22 | +This starts Alchemy in development mode, which will: |
| 23 | + |
| 24 | +- Emulate Cloudflare Workers and associated resources locally using Miniflare |
| 25 | +- Hot reload Workers when you make changes to your code |
| 26 | + |
| 27 | +### Watching Your Alchemy Configuration |
| 28 | + |
| 29 | +Alchemy does not watch your `alchemy.run.ts` file for changes. To automatically apply changes to your configuration, you can the watch mode associated with your runtime environment. For example: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Using bun's watch mode |
| 33 | +bun run --watch alchemy.run.ts |
| 34 | + |
| 35 | +# Using Node.js watch mode |
| 36 | +npx tsx --watch alchemy.run.ts |
| 37 | +``` |
| 38 | + |
| 39 | +Development mode is enabled automatically when the `--watch` flag is detected. |
| 40 | + |
| 41 | +### Programmatic Configuration |
| 42 | + |
| 43 | +You can also enable dev mode programmatically by setting the `dev` option: |
| 44 | + |
| 45 | +```typescript |
| 46 | +const app = await alchemy("my-app", { |
| 47 | + dev: true |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +## Configuration |
| 52 | + |
| 53 | +When running in dev mode, Alchemy runs your Cloudflare Workers locally using Miniflare, and will be available on a randomly selected port. You can specify the port by setting the `port` property on the `Worker` resource: |
| 54 | + |
| 55 | +```typescript |
| 56 | +const worker = await Worker("my-worker", { |
| 57 | + entrypoint: "worker.ts", |
| 58 | + dev: { |
| 59 | + port: 3000 |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +console.log(worker.url); // http://localhost:3000 |
| 64 | +``` |
| 65 | + |
| 66 | +## Website Development |
| 67 | + |
| 68 | +When using the `Website` resource in development mode, you can specify a custom development command that Alchemy will run locally: |
| 69 | + |
| 70 | +```typescript |
| 71 | +const website = await Website("my-website", { |
| 72 | + dev: { |
| 73 | + command: "npm run dev", |
| 74 | + url: "http://localhost:5173", |
| 75 | + } |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +If no command is specified, Alchemy will automatically detect and run the appropriate dev command based on your project's package manager: |
| 80 | + |
| 81 | +- **bun**: `bun vite dev` |
| 82 | +- **npm**: `npx vite dev` |
| 83 | +- **pnpm**: `pnpm vite dev` |
| 84 | +- **yarn**: `yarn vite dev` |
| 85 | + |
| 86 | +### Vite Integration |
| 87 | + |
| 88 | +For projects using Vite, Alchemy integrates with the [Cloudflare Vite plugin](https://developers.cloudflare.com/workers/development-testing/vite/) to provide enhanced local development capabilities. This integration enables better support for certain binding types when running locally. |
| 89 | + |
| 90 | +To enable Vite integration, configure your `vite.config.ts` with the Cloudflare plugin: |
| 91 | + |
| 92 | +```typescript |
| 93 | +import { cloudflare } from "@cloudflare/vite-plugin"; |
| 94 | +import { defineConfig } from "vite"; |
| 95 | + |
| 96 | +export default defineConfig({ |
| 97 | + plugins: [ |
| 98 | + cloudflare({ |
| 99 | + persistState: process.env.ALCHEMY_CLOUDFLARE_PERSIST_PATH |
| 100 | + ? { |
| 101 | + path: process.env.ALCHEMY_CLOUDFLARE_PERSIST_PATH, |
| 102 | + } |
| 103 | + : undefined, |
| 104 | + }), |
| 105 | + ], |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +The Vite integration provides improved support for the following binding types (marked with ✅ in the "Vite" column of the supported resources table below). |
| 110 | + |
| 111 | +## Bindings |
| 112 | + |
| 113 | +By default, Alchemy emulates resources such as [D1 Databases](../providers/cloudflare/d1-database.md), [KV Namespaces](../providers/cloudflare/kv-namespace.md), and [R2 Buckets](../providers/cloudflare/bucket.md) locally. |
| 114 | + |
| 115 | +Alchemy also supports [remote bindings](https://developers.cloudflare.com/workers/development-testing/#remote-bindings) for select resources. For resources that allow either local or remote execution, you can set the `dev` property on the resource to `{ remote: true }`: |
| 116 | + |
| 117 | +```typescript |
| 118 | +const db = await D1Database("my-db", { |
| 119 | + dev: { remote: true } |
| 120 | +}); |
| 121 | + |
| 122 | +const kv = await KVNamespace("my-kv", { |
| 123 | + dev: { remote: true } |
| 124 | +}); |
| 125 | + |
| 126 | +const r2 = await R2Bucket("my-r2", { |
| 127 | + dev: { remote: true } |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +Some resources only support remote execution, such as [AI Gateways](../providers/cloudflare/ai-gateway.md). These resources will automatically be run remotely, so usage will be billed the same as if you were running them in production. |
| 132 | + |
| 133 | +### Supported Resources |
| 134 | + |
| 135 | +The following bindings are supported in dev mode: |
| 136 | + |
| 137 | +| Resource | Local | Remote | Vite | |
| 138 | +|----------|-------|--------|------| |
| 139 | +| AI | ❌ | ✅ | ❌ | |
| 140 | +| Analytics Engine | ✅ | ❌ | ❌ | |
| 141 | +| Assets | ✅ | ❌ | ❌ | |
| 142 | +| Browser Rendering | ❌ | ✅ | ❌ | |
| 143 | +| D1 Database | ✅ | ✅ | ✅ | |
| 144 | +| Dispatch Namespace | ❌ | ✅ | ❌ | |
| 145 | +| Durable Object Namespace | ✅ | ❌ | ❌ | |
| 146 | +| Hyperdrive | ✅ | ❌ | ❌ | |
| 147 | +| Images | ✅ | ✅ | ❌ | |
| 148 | +| JSON | ✅ | ❌ | ❌ | |
| 149 | +| KV Namespace | ✅ | ✅ | ✅ | |
| 150 | +| Pipeline | ✅ | ❌ | ❌ | |
| 151 | +| Queue | ✅ | ✅ | ❌ | |
| 152 | +| R2 Bucket | ✅ | ✅ | ✅ | |
| 153 | +| Secret | ✅ | ❌ | ❌ | |
| 154 | +| Secret Key | ❌ | ❌ | ❌ | |
| 155 | +| Service | ✅ | ✅ | ❌ | |
| 156 | +| Vectorize Index | ❌ | ✅ | ❌ | |
| 157 | +| Version Metadata | ✅ | ❌ | ❌ | |
| 158 | +| Workflow | ✅ | ❌ | ❌ | |
| 159 | +| Text | ✅ | ❌ | ❌ | |
| 160 | + |
| 161 | +## Limitations |
| 162 | + |
| 163 | +- Hot reloading for Workers is only supported when the `entrypoint` property is set. To hot reload an inline script, you must use an external watcher to monitor your `alchemy.run.ts` file. |
| 164 | +- Local Workers can push to remote queues, but cannot consume from them. |
| 165 | +- Hyperdrive support is experimental. Hyperdrive configurations that use Cloudflare Access are not supported, and only configurations provisioned in the same `alchemy.run.ts` file will work. This is a [limitation from Cloudflare that is actively being worked on](https://developers.cloudflare.com/workers/development-testing/#unsupported-remote-bindings). |
| 166 | + |
| 167 | +## Best Practices |
| 168 | + |
| 169 | +1. **Use local resources for development** - Faster iteration and no API costs |
| 170 | +2. **Test with remote resources** - Validate integration before deployment |
| 171 | +3. **Leverage hot reloading** - Use entrypoint files for automatic rebuilds |
| 172 | +4. **Monitor build output** - Watch for compilation errors and warnings |
| 173 | +5. **Configure Worker ports explicitly** - Avoid conflicts in multi-worker setups |
| 174 | +6. **Use external watchers** - For automatic restarts when configuration changes |
0 commit comments