An Astro monorepo is one repository that holds several Astro sites plus the packages they share, usually managed with pnpm workspaces. Put each site in apps/, shared components and config in packages/, and reference the shared packages with workspace:* versions. Astro compiles .astro files from workspace packages without a build step, as long as the package declares astro as a peer dependency.
This guide sets up that structure from scratch, shows how to share components, styles and TypeScript config, and covers caching and deployment.
Key Takeaways
- Use pnpm workspaces with
apps/*for sites andpackages/*for shared code.- Ship shared
.astrocomponents as source. Addastroto the package’speerDependenciesso Astro compiles it.- Point
exportsin the shared package’spackage.jsonat the.astroand.tsfiles directly.- Turborepo or
pnpm -rruns builds across apps; Turborepo adds caching so unchanged apps skip the build.- Each app deploys separately; set the host’s root directory to
apps/<name>.
When an Astro monorepo makes sense
A monorepo is worth the setup when you have more than one site that shares code:
- A marketing site, a docs site (often Starlight) and a blog that should share a header, footer and design tokens
- Several client sites built on the same component library
- An Astro site next to a backend, CLI or API package it documents
If you have one site, a monorepo adds config without benefit. A single Astro project with a src/components/ folder is simpler. The Astro project structure guide covers that layout.
The folder layout
my-monorepo/
├── apps/
│ ├── web/ # marketing site (Astro)
│ └── docs/ # documentation (Astro + Starlight)
├── packages/
│ ├── ui/ # shared .astro components and styles
│ └── tsconfig/ # shared TypeScript config
├── package.json
├── pnpm-workspace.yaml
└── turbo.json # optional
The root package.json is private and holds workspace-wide scripts and dev tools:
{
"name": "my-monorepo",
"private": true,
"packageManager": "[email protected]",
"scripts": {
"dev": "pnpm -r --parallel dev",
"build": "pnpm -r build"
}
}
Set packageManager to the pnpm version you actually use. Hosts and Corepack read it to pick the right version in CI.
The workspace file tells pnpm where packages live:
# pnpm-workspace.yaml
packages:
- "apps/*"
- "packages/*"
pnpm is the usual choice for monorepos because its strict node_modules layout catches packages that use a dependency without declaring it. npm and Bun workspaces work too; the package manager comparison covers the trade-offs.
Create the apps
Create each site inside apps/:
# Run from the repo root
pnpm create astro@latest apps/web --template minimal --no-git
pnpm create astro@latest apps/docs --template starlight --no-git
pnpm install
Use --no-git so the generator doesn’t create nested Git repositories. Give each app a unique name in its package.json (for example @acme/web and @acme/docs), because pnpm filters by name.
Run one app with a filter:
pnpm --filter @acme/web dev
Share Astro components from a package
This is the part that trips people up. Create packages/ui/package.json:
{
"name": "@acme/ui",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
"./Header.astro": "./src/Header.astro",
"./Footer.astro": "./src/Footer.astro",
"./styles/tokens.css": "./src/styles/tokens.css"
},
"peerDependencies": {
"astro": "^7.0.0"
}
}
Two details matter:
exportspoints at source files. There is no build step. Astro compiles.astrofiles from the package the same way it compiles your own.astrois a peer dependency. When Astro starts, it scans dependencies and treats any package that depends onastro(or hasastroorastro-componentin itskeywords, or is namedastro-*) as an Astro package. Those packages are bundled and compiled by Vite instead of being loaded as external Node.js modules. Without that signal, importing a.astrofile from the package can fail with an unknown file extension error.
A shared component is a normal Astro component:
---
// packages/ui/src/Header.astro
interface Props {
siteName: string;
links: { href: string; label: string }[];
}
const { siteName, links } = Astro.props;
---
<header class="site-header">
<a href="/" class="brand">{siteName}</a>
<nav>
{links.map((link) => <a href={link.href}>{link.label}</a>)}
</nav>
</header>
<style>
.site-header {
display: flex;
justify-content: space-between;
padding: var(--space-4);
}
</style>
Add the package to an app and import it:
pnpm --filter @acme/web add @acme/ui@workspace:*
---
// apps/web/src/layouts/Base.astro
import Header from '@acme/ui/Header.astro';
import '@acme/ui/styles/tokens.css';
---
<html lang="en">
<body>
<Header siteName="Acme" links={[{ href: '/blog/', label: 'Blog' }]} />
<slot />
</body>
</html>
Scoped styles in the shared component work as usual, and edits to packages/ui hot-reload in every running app. The design tokens guide shows how to structure a shared tokens.css.
Share TypeScript config
Put a base config in packages/tsconfig:
{
"name": "@acme/tsconfig",
"version": "0.0.0",
"private": true,
"files": ["base.json"]
}
{
"extends": "astro/tsconfigs/strict",
"compilerOptions": {
"verbatimModuleSyntax": true
}
}
The second file is packages/tsconfig/base.json. Each app extends it after adding @acme/tsconfig@workspace:* as a dev dependency:
{
"extends": "@acme/tsconfig/base.json",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
Keep the include in each app, because paths in include resolve relative to the config that declares them. The TypeScript config guide explains the Astro presets.
Add Turborepo for caching
pnpm -r build builds every app every time. Turborepo adds task ordering and caching: if nothing in apps/docs or its dependencies changed, the docs build is restored from cache.
pnpm add -Dw turbo
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".astro/**"]
},
"dev": {
"cache": false,
"persistent": true
}
}
}
Save that as turbo.json, then change the root scripts to "build": "turbo run build" and "dev": "turbo run dev". "^build" builds a package’s dependencies first, which matters if a shared package has its own build step. For source-only packages like @acme/ui, it does nothing and costs nothing.
If your Astro build reads environment variables, list them in the task’s env array in turbo.json so a changed value invalidates the cache.
Deploy each app
Every app deploys on its own. On Cloudflare Pages, Netlify and Vercel, create one project per app and set:
| Setting | Value for apps/web |
|---|---|
| Root directory | apps/web |
| Build command | pnpm build (or turbo run build --filter=@acme/web) |
| Output directory | dist |
These hosts detect pnpm workspaces from the lockfile at the repo root and install the whole workspace, so workspace dependencies resolve. To skip builds for apps that didn’t change, use the host’s ignore-build setting with npx turbo-ignore, or path filters in your CI. The static deploy docs cover the per-host settings, and the GitHub Actions workflows guide shows a matrix build for several apps.
If you start each app from a theme, keep the theme as its own app and move shared pieces into packages/ui gradually. Komal Nakrani Astro themes are standalone Astro 7 projects, so they drop into apps/ without changes.
Common monorepo problems
Unknown file extension ".astro": the shared package isn’t detected as an Astro package. Addastroto itspeerDependencies.- Two copies of Astro: different apps pin different Astro majors, or a package lists
astroas a regular dependency. Runpnpm why astroand align versions; usepeerDependenciesin shared packages. - Changes in a package don’t show up: check that the app depends on it with
workspace:*and thatexportspoints at source, not a staledist/. - Type errors from shared components: make sure the app’s
tsconfig.jsonincludes.astro/types.d.ts, and runastro syncin each app.
The official Astro docs on project structure and pnpm workspaces docs are the references for each tool.
FAQ
Can Astro be used in a monorepo?
Yes. Astro works in pnpm, npm, Yarn and Bun workspaces. Put each site in its own folder with its own astro.config.mjs and package.json, and share code through workspace packages that declare astro as a peer dependency.
How do I share Astro components between projects?
Create a workspace package that exports the .astro files directly from its exports field, add astro to its peerDependencies, and install it in each app with workspace:*. Astro compiles the components from source, so the package needs no build step.
Do I need Turborepo for an Astro monorepo?
No. pnpm -r build and pnpm --filter are enough for two or three sites. Turborepo helps when builds get slow, because it caches each app’s output and skips apps whose files and dependencies haven’t changed.
How do I deploy one app from an Astro monorepo?
Create a project on your host for that app and set its root directory to the app folder, such as apps/web, with dist as the output directory. The host installs the workspace from the root lockfile, so shared packages resolve.
Should Starlight live in the same monorepo as my marketing site?
It can. A Starlight docs app in apps/docs can import the same header, footer and tokens as your marketing site through component overrides, which keeps both sites visually consistent while they deploy separately.