| 1 | # Cloudflare Workers |
| 2 | |
| 3 | One of the simplest ways to deploy your `server` is through Cloudflare Workers, which are lightweight, fast, and very inexpensive. |
| 4 | |
| 5 | ## Deployment |
| 6 | |
| 7 | ::::steps |
| 8 | |
| 9 | ### Login to Cloudlfare |
| 10 | |
| 11 | Using the `wrangler` CLI login to your Cloudflare account to authorize it |
| 12 | |
| 13 | ```bash [!terminal] |
| 14 | bunx wrangler login |
| 15 | ``` |
| 16 | |
| 17 | ### Add `wrangler.jsonc` File |
| 18 | |
| 19 | Create a new file called `wrangler.jsonc` in the root of your `server` package and paste in the following template. |
| 20 | |
| 21 | ```jsonc |
| 22 | { |
| 23 | "$schema": "node_modules/wrangler/config-schema.json", |
| 24 | "name": "server", |
| 25 | "main": "src/index.ts", |
| 26 | "compatibility_date": "2025-05-25" |
| 27 | // "compatibility_flags": [ |
| 28 | // "nodejs_compat" |
| 29 | // ], |
| 30 | // "vars": { |
| 31 | // "MY_VAR": "my-variable" |
| 32 | // }, |
| 33 | // "kv_namespaces": [ |
| 34 | // { |
| 35 | // "binding": "MY_KV_NAMESPACE", |
| 36 | // "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 37 | // } |
| 38 | // ], |
| 39 | // "r2_buckets": [ |
| 40 | // { |
| 41 | // "binding": "MY_BUCKET", |
| 42 | // "bucket_name": "my-bucket" |
| 43 | // } |
| 44 | // ], |
| 45 | // "d1_databases": [ |
| 46 | // { |
| 47 | // "binding": "MY_DB", |
| 48 | // "database_name": "my-database", |
| 49 | // "database_id": "" |
| 50 | // } |
| 51 | // ], |
| 52 | // "ai": { |
| 53 | // "binding": "AI" |
| 54 | // }, |
| 55 | // "observability": { |
| 56 | // "enabled": true, |
| 57 | // "head_sampling_rate": 1 |
| 58 | // } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### Update `package.json` Files |
| 63 | |
| 64 | Add the following items to your `server/package.json` file |
| 65 | |
| 66 | ```json |
| 67 | { |
| 68 | "name": "server", |
| 69 | "version": "0.0.1", |
| 70 | "main": "dist/index.js", |
| 71 | "types": "dist/index.d.ts", |
| 72 | "scripts": { |
| 73 | "build": "tsc", |
| 74 | "dev": "bun run --hot src/index.ts", // [!code --] |
| 75 | "dev": "wrangler dev", // [!code ++] |
| 76 | "deploy": "wrangler deploy --minify", // [!code ++] |
| 77 | "cf-typegen": "wrangler types --env-interface CloudflareBindings" // [!code ++] |
| 78 | }, |
| 79 | "dependencies": { |
| 80 | "hono": "^4.7.7", |
| 81 | "shared": "workspace:*" |
| 82 | }, |
| 83 | "devDependencies": { |
| 84 | "@types/bun": "latest", |
| 85 | "wrangler": "^4.4.0" // [!code ++] |
| 86 | } |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | Then update `scripts` section of the root `package.json` for your bhvr project |
| 91 | |
| 92 | ```json |
| 93 | "scripts": { |
| 94 | "dev": "turbo dev", |
| 95 | "dev:client": "turbo dev --filter=client", |
| 96 | "dev:server": "turbo dev --filter=server", |
| 97 | "build": "turbo build", |
| 98 | "build:client": "turbo build --filter=client", |
| 99 | "build:server": "turbo build --filter=server", |
| 100 | "deploy:server": "cd server && bun run deploy", // [!code ++] |
| 101 | "lint": "turbo lint", |
| 102 | "type-check": "turbo type-check", |
| 103 | "test": "turbo test", |
| 104 | "postinstall": "turbo build --filter=shared --filter=server" |
| 105 | }, |
| 106 | ``` |
| 107 | |
| 108 | ### Deploy |
| 109 | |
| 110 | Install dependencies for your updated `server/package.json` then run the deploy command |
| 111 | |
| 112 | ```bash [!terminal] |
| 113 | bun install |
| 114 | bun run deploy:server |
| 115 | ``` |
| 116 | |
| 117 | :::: |
| 118 | |
| 119 | ::::tip |
| 120 | [Check out the Cloudflare Docs for more info and tips](https://developers.cloudflare.com/workers/) |
| 121 | :::: |