This template provides a fully featured monorepo for managing multiple Cloudflare Workers.
Managing multiple related services (like Cloudflare Workers) in separate repositories can become complex. A monorepo approach offers several advantages:
- Simplified dependency management -
pnpm workspacesallow you to manage dependencies across all your workers and shared packages from a single place. The toolsyncpack(configured via.syncpackrc.cjs) help keep versions consistent. - Code sharing and reuse - Easily create and share common logic, types, and utilities between workers by placing them in the
packages/directory. Changes to shared code are immediately available to all consumers. - Atomic commits - Changes affecting multiple workers or shared libraries can be committed together, making the history easier to understand and reducing the risk of inconsistencies.
- Consistent tooling - Apply the same build, test, linting, and formatting configurations (e.g., via Turborepo in
turbo.jsonand shared configs inpackages/) across all projects, ensuring consistent tooling and code quality across Workers. - Streamlined CI/CD - A single pipeline (like the ones in
.github/workflows/) can build, test, and deploy all Workers, simplifying the release process. - Easier refactoring - Refactoring code that spans multiple workers or shared packages is significantly easier within a single repository.
You can bootstrap a new monorepo using this template by running:
npm create workers-monorepo@latest- node.js v22 or later
- pnpm v10 or later
- bun 1.2 or later
- rg (ripgrep) - optional, but recommended for shell formatting
- shfmt - optional, but recommended for shell formatting
- mise - optional, but recommended for tool management
Install Dependencies:
just installRun Development Server:
just devCreate a New Worker:
Use the built-in generator to scaffold a new Cloudflare Workers application:
just new-workerThis will guide you throught he setup process of creating a new application within the apps/ directory.
Deploy all Workers:
just deployNote: This will also deploy the example application in apps/example-worker-echoback. If you don't want to deploy that Worker, simply remove the deploy script from apps/example/workers/echoback/package.json.
This monorepo is organized as follows:
apps/- Contains individual Cloudflare Worker applications. Each subdirectory is typically a deployable unit.example-worker-echoback- An example worker demonstrating basic functionality.
packages/- Shared libraries, utilities, and configurations used across multiple applications.packages/tools/- A package containing various scripts and a CLI for developing the monorepo.- Each Workers application's package.json scripts point to scripts within
packages/tools/bin/. This makes it easier to keep scripts consistent across Workers.
- Each Workers application's package.json scripts point to scripts within
turbo/- Containsturbo gentemplatesfetch-worker: A basic Cloudflare Worker template.fetch-worker-vite: A Cloudflare Worker template using Vite for bundling and development.
Justfile- Defines convenient aliases for common development tasks.pnpm-workspace.yaml- Defines the pnpm workspace structure.turbo.json- Configures Turborepo build and task execution..syncpackrc.cjs- Configuressyncpackfor managing and synchronizing dependency versions across packages in the monorepo.- The included configuration ensures that dependencies are all kept in sync and use a pinned version so that we can choose when to update dependencies.
This repository uses a Justfile to provide easy access to common commands. You can explore all available commands by running just --list.
Here are some key commands:
just- Show a list of available commands.just install- Install all dependencies.just dev- Start development server (context-aware: runsbun runx dev).just build- Build all workers (runsbun turbo build).just test- Run tests (runsbun vitest).just check- Check code quality: deps, lint, types, format (runsbun runx check).just fix- Fix code issues: deps, lint, format, workers-types (runsbun runx fix).just preview- Run Workers in preview mode.just deploy- Deploy workers (runsbun turbo deploy).just cs- Create a new changeset for versioning.just update deps- Update dependencies across the monorepo with syncpack.just update pnpm- Update pnpm version.just update turbo- Update turbo version.just new-worker(alias:just gen) - Generate a new Cloudflare Worker.just new-package- Generate a new package for sharing code.
For a complete list of available commands, run just or see the Justfile for more details.
This repository includes GitHub Actions workflows defined in the .github/workflows directory:
-
branches.yml(Branches Workflow):- Triggered on pushes to any branch except
main. - Installs dependencies with pnpm.
- Runs checks/tests (
bun runx ci check)
- Triggered on pushes to any branch except
-
release.yml(Release Workflow):- Triggered on pushes to the
mainbranch. - Contains two jobs:
test-and-deploy: Installs dependencies, runs checks/tests (bun turbo check:ci), and then deploys all workers (bun turbo deploy). This step requires theCLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_IDsecrets to be configured in your repository's GitHub secrets.create-release-pr: Uses Changesets to create a pull request that compiles changelogs and bumps package versions. This PR is primarily for documentation and versioning, as deployment happens directly on merge tomain.
- Triggered on pushes to the