Overview
The BE Monorepo is built using Bun workspaces, providing a modern, fast, and efficient way to manage multiple packages and applications in a single repository. This architecture enables code sharing, consistent tooling, and streamlined dependency management across the entire project.Workspace Structure
The monorepo is organized into two main directories:Workspace Configuration
The rootpackage.json defines the workspace structure:
package.json
The
workspaces field tells Bun to treat all directories under apps/ and packages/ as individual packages within the monorepo.Bun Workspace Features
Workspace Protocol
Packages use theworkspace:* protocol to reference each other, ensuring they always use the local version:
apps/hono/package.json
Isolated Linker
The project uses Bun’s isolated linker for better dependency isolation:bunfig.toml
- Installs exact versions (no semver ranges)
- Provides isolated node_modules for better predictability
- Prevents dependency conflicts between packages
Apps Directory
@workspace/hono
The Hono application is the main backend API server:- Hono 4 web framework
- PostgreSQL with Drizzle ORM
- Better Auth authentication
- OpenTelemetry observability
- Port 3333 (development)
@workspace/core- Shared utilities and services@workspace/typescript-config- TypeScript configuration
Packages Directory
@workspace/core
Shared code used across applications:packages/core/package.json
@workspace/typescript-config
Shared TypeScript configurations for consistent typing across the monorepo.base.json- Base configuration for all packagesnode.json- Node.js-specific configuration
Workspace Commands
The rootpackage.json provides scripts that work across the monorepo:
Filtering Commands
Run commands for specific packages using Bun’s--filter flag:
Parallel Execution
Run scripts across multiple packages in parallel:Workspace-wide Scripts
package.json
Benefits of This Architecture
1. Code Sharing
Share code seamlessly between applications without publishing to npm:apps/hono/src/routes/users.ts
2. Consistent Tooling
All packages share the same:- TypeScript configuration
- Linting rules (Biome)
- Formatting standards
- Build tools
3. Dependency Management
Singlebun.lock file for the entire monorepo:
- Faster installs
- Consistent versions
- Reduced disk space
- Simplified security audits
4. Atomic Changes
Make changes across multiple packages in a single commit:5. Type Safety Across Boundaries
TypeScript types flow seamlessly between packages:packages/core/src/types/user.ts
apps/hono/src/routes/users.ts
Path Aliases
Workspace packages can be imported using TypeScript path aliases:apps/hono/tsconfig.json
Best Practices
Keep packages focused
Keep packages focused
Each package should have a single, clear responsibility. The
@workspace/core package contains utilities used across apps, while app-specific code stays in the app directory.Use workspace protocol
Use workspace protocol
Always use
workspace:* for internal dependencies to ensure you’re using the local version during development.Export intentionally
Export intentionally
Use the
exports field to control what other packages can import, preventing accidental dependencies on internal implementation details.Share configurations
Share configurations
Adding New Packages
To add a new package to the monorepo:-
Create the package directory:
-
Add a
package.json: -
Install dependencies in the root:
-
Reference it in other packages:
Next Steps
Environment Variables
Learn how to configure environment variables across the monorepo
TypeScript Config
Understand the shared TypeScript configuration
