Skip to content

tripheo0412/react-nestjs-monorepo-boilerplate

Repository files navigation

React + NestJS Monorepo Boilerplate

A production-ready monorepo starter with React 19, NestJS 11, and TypeScript. Built for scalability, developer experience, and modern best practices.

🎯 Using This Template

This repository is designed to be used as a GitHub template. When creating a new project from this template, you'll need to rename various parts to match your project. See TEMPLATE-SETUP.md for detailed instructions on renaming everything properly.

For AI Agents: If you're an AI assistant helping set up a new project from this template, please refer to the "AI Agent Instructions" section in CLAUDE.md for specific renaming guidelines.

πŸš€ Features

This repository contains a Turborepo setup using pnpm workspaces with:

Applications

  • frontend-boilerplate – React 19 app with:

    • ⚑ Vite for lightning-fast development
    • 🧭 Tanstack Router for type-safe, file-based routing
    • 🎨 Tailwind CSS v4 with automatic class sorting
    • 🎯 shadcn/ui component library (customizable)
    • πŸ“š Storybook for component development
    • πŸ§ͺ Vitest + React Testing Library (80% coverage requirement)
    • πŸ”§ ESLint + Prettier integration
    • πŸ” Authentication with JWT token management
    • πŸ“‘ Axios-based API client with interceptors
    • 🎣 React Query for server state management
    • πŸ“± Responsive design with mobile-first approach
  • backend-boilerplate – NestJS 11 API with:

    • πŸ—οΈ Modular architecture with feature-based modules
    • πŸ—„οΈ PostgreSQL + MikroORM with migrations
    • πŸ” JWT authentication with refresh tokens
    • ⚑ Redis caching with Keyv
    • πŸ“– Swagger/OpenAPI documentation
    • πŸ§ͺ Jest for testing (80% coverage requirement)
    • πŸ›‘οΈ Rate limiting & security (Helmet, CORS)
    • πŸ“Š OpenTelemetry integration for observability
    • πŸ“ Structured logging with Pino
    • πŸ” Health checks with Terminus
    • 🎯 Request validation with class-validator
    • 🏷️ Environment variables validation with Joi

Shared Packages

  • types – Shared TypeScript types between frontend and backend
  • shared-lib-boilerplate – Template for creating shared libraries

πŸ› οΈ Tech Stack

  • Monorepo: TurboRepo 2.5.3 + pnpm workspaces
  • Frontend: React 19.1.0, Vite 7.0.0, TypeScript 5.8.3, Tanstack Router 1.122.0, Tailwind CSS v4.1.11
  • Backend: NestJS 11.1.2, TypeScript 5.8.3, MikroORM 6.4.15, PostgreSQL 17, Redis 7
  • Testing: Vitest 3.2.4, Jest 29.7.0, React Testing Library 16.3.0, Storybook 9.0.14
  • Code Quality: ESLint 9.27.0, Prettier 3.5.3, Husky 9.1.7, lint-staged 16.1.0

πŸ›οΈ Architecture Highlights

  • Type Safety: End-to-end type safety with shared types package
  • Authentication: Complete auth flow with JWT tokens, refresh tokens, and protected routes
  • API Client: Centralized API client with automatic token refresh and error handling
  • Testing: Comprehensive testing setup with coverage requirements
  • Developer Experience: Hot reload, automatic formatting, pre-commit hooks
  • Production Ready: Environment validation, logging, monitoring, error handling
  • Scalable: Modular architecture, code splitting, lazy loading

πŸ“‹ Requirements

  • Node.js v22 LTS or higher
  • pnpm v10.5.2 (specified via packageManager in package.json)
  • PostgreSQL 17 (for backend) - or use Docker Compose
  • Redis 7 (for backend caching) - or use Docker Compose

πŸš€ Quick Start

# Clone the repository
git clone https://github.com/your-repo/react-nestjs-monorepo-boilerplate.git
cd react-nestjs-monorepo-boilerplate

# Install dependencies
pnpm install

# Option 1: Use Docker for databases (recommended)
docker-compose -f docker-compose.local.yml up -d

# Option 2: Use your own PostgreSQL and Redis instances
# Set up environment variables for backend
cp apps/backend-boilerplate/.env.example apps/backend-boilerplate/.env
# Edit .env with your database credentials

# Run database migrations
cd apps/backend-boilerplate && pnpm migration:run && cd ../..

# Run development servers
pnpm dev

The apps will be available at:

πŸ“ Scripts

Monorepo-wide commands:

  • pnpm dev – Run all apps in development mode
  • pnpm build – Build all apps for production
  • pnpm test – Run tests in all apps
  • pnpm lint – Run ESLint across all apps
  • pnpm format – Format code with Prettier
  • pnpm type-check – TypeScript type checking
  • pnpm build-type – Build shared types package
  • pnpm build-libs – Build all shared libraries

Frontend-specific (in apps/frontend-boilerplate/):

  • pnpm dev – Start React dev server
  • pnpm build – Build for production
  • pnpm test – Run Vitest tests
  • pnpm test:coverage – Run tests with coverage
  • pnpm storybook – Start Storybook
  • pnpm lint – Run ESLint
  • pnpm format – Format code with Prettier

Backend-specific (in apps/backend-boilerplate/):

  • pnpm dev – Start NestJS with hot reload
  • pnpm test – Run unit tests
  • pnpm test:e2e – Run e2e tests
  • pnpm test:coverage – Run tests with coverage

πŸ“ Project Structure

β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ frontend-boilerplate/     # React 19 + Vite app
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ apiClient/        # API client setup
β”‚   β”‚   β”‚   β”œβ”€β”€ components/       # UI components
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ layout/       # Layout components
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ pages/        # Page components
β”‚   β”‚   β”‚   β”‚   └── ui/           # Reusable UI components
β”‚   β”‚   β”‚   β”œβ”€β”€ contexts/         # React contexts
β”‚   β”‚   β”‚   β”œβ”€β”€ hooks/            # Custom React hooks
β”‚   β”‚   β”‚   β”œβ”€β”€ routes/           # Tanstack Router pages
β”‚   β”‚   β”‚   β”œβ”€β”€ lib/              # Utilities
β”‚   β”‚   β”‚   └── test/             # Test setup
β”‚   β”‚   └── .storybook/           # Storybook configuration
β”‚   └── backend-boilerplate/      # NestJS 11 API
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ modules/          # Feature modules
β”‚       β”‚   β”‚   β”œβ”€β”€ auth/         # Authentication module
β”‚       β”‚   β”‚   β”œβ”€β”€ users/        # Users module
β”‚       β”‚   β”‚   └── health/       # Health checks
β”‚       β”‚   β”œβ”€β”€ entities/         # Database entities
β”‚       β”‚   β”œβ”€β”€ configs/          # Configuration
β”‚       β”‚   β”œβ”€β”€ shared/           # Shared utilities
β”‚       β”‚   └── migrations/       # Database migrations
β”‚       └── test/                 # E2E tests
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ types/                    # Shared TypeScript types
β”‚   └── shared-lib-boilerplate/   # Template for shared libraries
β”œβ”€β”€ docs/                         # Documentation
β”œβ”€β”€ CLAUDE.md                     # AI assistant guidelines
β”œβ”€β”€ AI-CONTEXT.md                 # Technical context
β”œβ”€β”€ TEMPLATE-SETUP.md             # Template usage guide
└── turbo.json                    # TurboRepo configuration

🎨 Key Patterns & Conventions

Naming Conventions

  • Entities: Must have "Entity" suffix (e.g., UserEntity)
  • DTOs: Shared as TypeScript interfaces in packages/types
  • Database: PostgreSQL uses snake_case, TypeScript uses camelCase
  • Components: PascalCase for components, camelCase for utilities

Code Organization

  • Frontend: Feature-based with separation of concerns (components, hooks, contexts)
  • Backend: Module-based architecture following NestJS best practices
  • Shared Types: Centralized in packages/types for consistency

Testing Requirements

  • Minimum 80% code coverage for all new code
  • Every component must have tests and Storybook stories
  • Backend services must have comprehensive unit tests

πŸ”§ Development Guidelines

Frontend Component Development

Every new component MUST have:

  1. Component file - The React component
  2. Test file - Unit tests using Vitest and React Testing Library
  3. Story file - Storybook stories for all component states

Example structure:

src/components/ui/
β”œβ”€β”€ Button.tsx
β”œβ”€β”€ Button.stories.tsx
└── __tests__/
    └── Button.test.tsx

Backend Service Development

Every new service/controller MUST have:

  1. Service/Controller - Business logic with proper decorators
  2. DTOs - Data Transfer Objects with validation
  3. Tests - Unit tests with minimum 80% coverage
  4. Swagger docs - API documentation decorators

πŸ”’ Pre-commit Hooks

This project uses Husky and lint-staged to ensure code quality. On every commit:

  • ESLint checks are run on affected files
  • TypeScript type checking is performed
  • Prettier formats all staged files

See docs/HUSKY_LINT_STAGED.md for details.

🚒 CI/CD Pipeline

The project includes a comprehensive GitHub Actions workflow that runs on every push and PR:

  • Build & Type Check: Ensures all packages build successfully
  • Linting & Formatting: Enforces code style consistency
  • Testing: Runs all tests with coverage requirements
  • Services: Spins up PostgreSQL and Redis for integration tests
  • Caching: Optimized with pnpm and Turbo caching

🐳 Docker Development

For local development, use the included Docker Compose setup:

# Start PostgreSQL (with pgvector) and Redis
docker-compose -f docker-compose.local.yml up -d

# Stop services
docker-compose -f docker-compose.local.yml down

# Stop and remove volumes (fresh start)
docker-compose -f docker-compose.local.yml down -v

Services:

  • PostgreSQL 17 with pgvector extension (port 5432)
  • Redis 7 (port 6379)

πŸ“š Storybook

The frontend includes a comprehensive Storybook setup:

# Run Storybook (from frontend directory or root)
pnpm storybook

# Build Storybook
pnpm build-storybook

Features:

  • Component documentation and playground
  • Accessibility testing with a11y addon
  • Interaction testing with play functions
  • Mock router for Tanstack Router components
  • Global decorators for providers (Auth, Query Client)

⚑ Turbo Monorepo

This project uses TurboRepo for efficient monorepo management:

  • Parallel Execution: Tasks run in parallel when possible
  • Smart Caching: Only rebuilds what changed
  • Pipeline Optimization: Dependency-aware task scheduling

Key pipelines in turbo.json:

  • build: Builds all packages in dependency order
  • test: Runs tests with caching
  • lint/format: Code quality checks
  • type-check: TypeScript validation

πŸ“š Documentation

🀝 Contributing

  1. Create a feature branch
  2. Make your changes following the guidelines
  3. Ensure all tests pass (pnpm test:coverage)
  4. Run formatting (pnpm format)
  5. Create a pull request

πŸ“„ License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published