A modern, fully TypeScript and fully decoupled full-stack application boilerplate featuring a backend and frontend with shared type definitions.
This boilerplate addresses several key challenges in modern full-stack development:
The biggest challenge was ensuring complete type safety between backend and frontend. Traditional approaches often lead to:
- Manual type duplication and maintenance
- Runtime errors due to type mismatches
- Inconsistent API contracts
- Time-consuming debugging when types drift apart
Solution: Automatic type synchronization with a single source of truth (shared/Types.ts) that keeps both backend and frontend perfectly in sync.
Backend endpoints should be reusable across different clients:
- Mobile apps (React Native, Flutter, etc.)
- Cron job services and background workers
- Third-party integrations and webhooks
- Multiple frontend applications
Solution: Fully decoupled architecture where the backend is a standalone API service that can be consumed by any client, with shared types ensuring consistent contracts across all consumers.
This boilerplate is designed with a fully decoupled architecture where the backend and frontend are completely independent applications that can be developed, deployed, and scaled separately.
- Backend: Express.js/Node.js with TypeScript
- Frontend: React with TypeScript and Vite
- Shared Types: Automatic type synchronization between backend and frontend
- Git Hooks: Pre-push validation to ensure type consistency
- Modern Tooling: Full TypeScript support, ESLint, and optimized build configurations
project-boilerplate/
βββ backend/ # Express.js/Node.js with TypeScript
β βββ src/
β β βββ server.ts # Main Express server file
β β βββ shared/ # Shared types (auto-synced)
β βββ package.json
βββ frontend/ # React with TypeScript and Vite
β βββ src/
β β βββ App.tsx # Main React component
β β βββ shared/ # Shared types (auto-synced)
β βββ package.json
βββ shared/ # Source of truth for shared types
β βββ Types.ts
βββ scripts/ # Build and setup scripts
βββ copy-shared.js # Type synchronization script
βββ pre-push.sh # Git pre-push hook
- Node.js (v18 or higher)
- npm or yarn
- Git
-
Clone the repository
git clone <your-repo-url> cd project-boilerplate
-
Install dependencies
# Install backend dependencies cd backend npm install # Install frontend dependencies cd ../frontend npm install
-
Configure Git hooks
β οΈ IMPORTANT# Make the setup script executable chmod +x scripts/setup-hooks.sh # Run the setup script ./scripts/setup-hooks.sh
Note: You must configure the Git hooks for the type synchronization to work properly. The setup script will install the pre-push hook that ensures type consistency between backend and frontend.
-
Start development servers
# Terminal 1: Start backend cd backend npm run dev # Terminal 2: Start frontend cd frontend npm run dev
The boilerplate automatically keeps types synchronized between backend and frontend:
- Source of Truth:
shared/Types.tscontains all shared type definitions - Automatic Sync: The
copy-shared.jsscript copies types to bothbackend/src/shared/andfrontend/src/shared/ - Pre-push Validation: Git hooks ensure types are always in sync before pushing
- Edit
shared/Types.ts - The types will be automatically copied to both backend and frontend
- Import and use in your code:
// In backend or frontend import { YourType } from './shared/Types';
- Location:
backend/ - Framework: Express.js with Node.js and TypeScript
- Scripts:
npm run dev: Start development servernpm run build: Build for productionnpm start: Start production server
- Location:
frontend/ - Framework: React with TypeScript and Vite
- Scripts:
npm run dev: Start Vite dev servernpm run build: Build for productionnpm run preview: Preview production build
The boilerplate includes a pre-push hook that:
- Ensures shared types are synchronized
- Prevents pushing if types are out of sync
- Runs automatically before each
git push
If the automatic setup doesn't work, manually configure:
# Copy the pre-push hook
cp scripts/pre-push.sh .git/hooks/pre-push
# Make it executable
chmod +x .git/hooks/pre-pushcd backend
npm run build
npm startcd frontend
npm run buildThe built frontend files will be in frontend/dist/ and can be served by any static file server.
- Backend: Add to
backend/package.json - Frontend: Add to
frontend/package.json - Shared: Add to both if needed across the stack
- Backend: Edit
backend/tsconfig.json - Frontend: Edit
frontend/vite.config.tsandfrontend/tsconfig.json
- Ensure Git hooks are properly configured
- Run the copy script manually:
node scripts/copy-shared.js - Check file permissions on the setup script
- Verify all dependencies are installed in both directories
- Check TypeScript configuration files
- Ensure shared types are properly imported
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Ensure types are synchronized
- Submit a pull request
Happy coding! π