Running Locally
This guide explains how to start and run BookWish services on your local development machine.
Prerequisites
Before starting, ensure you have:
- Completed Environment Setup
- Configured Environment Variables
- PostgreSQL and Redis running locally (or configured remote services)
Starting the Backend API
The backend must be running for the mobile app and web projects to function.
Start Backend Server
cd backend
npm run dev
Expected output:
[nodemon] starting `ts-node src/index.ts`
Server running on port 3000
Database connected ✓
Redis connected ✓
Firebase initialized ✓
Available at: http://localhost:3000
Backend Development Scripts
# Start development server with auto-reload
npm run dev
# Build TypeScript to JavaScript
npm run build
# Start production build
npm start
# Run tests
npm test
# Type check without building
npm run lint
# Prisma commands
npm run prisma:migrate # Run database migrations
npm run prisma:generate # Generate Prisma client
npm run prisma:seed # Seed database with sample data
Verify Backend is Running
Test the health endpoint:
curl http://localhost:3000/health
Expected response:
{
"status": "ok",
"timestamp": "2025-12-08T12:00:00.000Z"
}
Running the Flutter App
The Flutter app can run on iOS Simulator, Android Emulator, or web browser.
iOS (macOS only)
Start iOS Simulator:
open -a Simulator
Run the app:
cd app
flutter run
Flutter will automatically detect the iOS Simulator and install the app.
Android
Start Android Emulator:
- Open Android Studio > AVD Manager
- Select an emulator and click "Run"
Or from command line:
# List available emulators
emulator -list-avds
# Start a specific emulator
emulator -avd Pixel_5_API_33
Run the app:
cd app
flutter run
Web
cd app
flutter run -d chrome
Available at: Flutter assigns a random port - check the terminal output
Physical Devices
iOS (requires macOS and Xcode):
- Connect iPhone via USB
- Enable Developer Mode on iPhone: Settings > Privacy & Security > Developer Mode
- Trust your Mac on the iPhone when prompted
- Run:
flutter run
Android:
- Enable Developer Options on Android device
- Enable USB Debugging
- Connect via USB
- Run:
flutter run
Flutter Development Commands
# Run on specific device
flutter run -d <device-id>
# List available devices
flutter devices
# Run with specific flavor (if configured)
flutter run --flavor dev
# Clean build cache
flutter clean
# Get dependencies
flutter pub get
# Run code generation (Riverpod)
flutter pub run build_runner build --delete-conflicting-outputs
# Watch mode for code generation
flutter pub run build_runner watch --delete-conflicting-outputs
# Run tests
flutter test
# Analyze code
flutter analyze
Hot Reload & Hot Restart
When the app is running:
- Hot Reload: Press
rin the terminal (applies code changes without restarting) - Hot Restart: Press
R(restarts the app completely) - Quit: Press
q
Hot reload preserves app state and is very fast. Use hot restart if hot reload doesn't work (e.g., after changing dependencies or app initialization).
Running Web Projects
Customer-Facing Website (Next.js)
cd web
npm run dev
Available at: http://localhost:3001 (default Next.js port 3000, adjusted to avoid conflict)
Bookstore Websites (Next.js)
cd stores
npm run dev
Available at: http://localhost:3002
Access a specific store:
- Development:
http://localhost:3002?store=store-slug - Or configure local DNS for testing custom domains
Web Development Commands
# Start development server
npm run dev
# Build for production
npm run build
# Start production server
npm start
# Run linter
npm run lint
Running All Services Together
For full-stack development, run these in separate terminal windows/tabs:
Terminal 1: Backend
cd backend && npm run dev
Terminal 2: Flutter (choose one)
# iOS
cd app && flutter run
# Android
cd app && flutter run
# Web
cd app && flutter run -d chrome
Terminal 3: Web Project (optional)
cd web && npm run dev
Terminal 4: Stores Project (optional)
cd stores && npm run dev
Terminal 5: Code Generation (optional, for Flutter)
cd app && flutter pub run build_runner watch --delete-conflicting-outputs
Terminal 6: Prisma Studio (optional, for database browsing)
cd backend && npx prisma studio
Service Verification
Check Backend Health
# Health check
curl http://localhost:3000/health
# Test authentication endpoint
curl http://localhost:3000/v1/auth/guest -X POST
# View API documentation (if configured)
curl http://localhost:3000/v1/docs
Check Database Connection
cd backend
npx prisma studio
Opens Prisma Studio at http://localhost:5555 for visual database browsing.
Check Redis Connection
redis-cli ping
# Should return: PONG
# Monitor Redis activity
redis-cli monitor
Check Flutter App
The app should connect to the backend API automatically. Check the debug console for:
API connected: http://localhost:3000
Development Workflow
Typical Development Session
-
Start backend (Terminal 1)
cd backend && npm run dev -
Start code generation (Terminal 2)
cd app && flutter pub run build_runner watch -
Run Flutter app (Terminal 3)
cd app && flutter run -
Make changes:
- Edit code in your IDE
- Save files
- Press
rin the Flutter terminal for hot reload - Backend auto-reloads with nodemon
-
Test changes:
- Interact with the app
- Check backend logs
- Use Prisma Studio to inspect database
Code Generation Workflow
When adding new Riverpod providers or modifying existing ones:
- Add/modify provider code with
@riverpodannotation - Save the file
build_runner watchautomatically regenerates the.g.dartfile- Press
rin Flutter terminal to hot reload
Manual generation:
flutter pub run build_runner build --delete-conflicting-outputs
Database Migration Workflow
When modifying the Prisma schema:
- Edit
backend/prisma/schema.prisma - Create migration:
cd backend
npm run prisma:migrate
# Enter migration name when prompted - Migration is automatically applied
- Prisma Client is regenerated
Testing API Changes
Use curl, Postman, or Insomnia:
# Create guest user
curl -X POST http://localhost:3000/v1/auth/guest
# Get books
curl -X GET http://localhost:3000/v1/books
# Search books
curl -X GET "http://localhost:3000/v1/books/search?q=gatsby"
Port Reference
| Service | Port | URL |
|---|---|---|
| Backend API | 3000 | http://localhost:3000 |
| Web (Next.js) | 3001 | http://localhost:3001 |
| Stores (Next.js) | 3002 | http://localhost:3002 |
| Prisma Studio | 5555 | http://localhost:5555 |
| Flutter Web | Auto | (auto-assigned port) |
| PostgreSQL | 5432 | localhost:5432 |
| Redis | 6379 | localhost:6379 |
Common Development Tasks
Reset Database
cd backend
# Drop all tables and re-run migrations
npx prisma migrate reset
# Seed with sample data
npm run prisma:seed
Warning: This deletes all data. Use only in development.
Clear Redis Cache
redis-cli FLUSHALL
Rebuild Flutter App
cd app
# Clean build artifacts
flutter clean
# Get dependencies
flutter pub get
# Regenerate code
flutter pub run build_runner build --delete-conflicting-outputs
# Run app
flutter run
View Backend Logs
Backend logs are printed to the terminal. For structured logging:
cd backend
npm run dev | tee logs.txt
Debugging
Flutter:
- VS Code: Use Debug > Start Debugging (F5)
- Android Studio: Click the debug icon
- DevTools:
flutter runautomatically opens DevTools
Backend:
- VS Code: Add debug configuration in
.vscode/launch.json - Use
console.log()orlogger.info()for logging
Common Issues
Port Already in Use
Error: Error: listen EADDRINUSE: address already in use :::3000
Solution:
# Find process using port 3000
lsof -ti:3000
# Kill the process
kill -9 $(lsof -ti:3000)
# Or use a different port
PORT=3001 npm run dev
Flutter App Not Connecting to Backend
- Check backend is running on
http://localhost:3000 - Verify API URL in Flutter app configuration
- For iOS Simulator: Use
localhost - For Android Emulator: Use
10.0.2.2instead oflocalhost - For physical device: Use your computer's IP address
Build Runner Conflicts
Error: Conflicting outputs were detected
Solution:
flutter pub run build_runner build --delete-conflicting-outputs
iOS Pod Install Fails
cd app/ios
rm -rf Pods Podfile.lock
pod install --repo-update
cd ../..
Database Migration Issues
If migrations fail, check:
- PostgreSQL is running
DATABASE_URLis correct- Database exists and is accessible
Reset and retry:
cd backend
npx prisma migrate reset
npm run prisma:migrate
Performance Tips
Backend
- Use
npm run devfor auto-reload (faster than manual restarts) - Keep Redis running to avoid reconnection delays
- Use Prisma Studio instead of raw SQL queries for debugging
Flutter
- Use hot reload (
r) instead of hot restart when possible - Run
build_runner watchin a separate terminal - Use Flutter DevTools for performance profiling
- Keep the emulator/simulator running between sessions
Web Projects
- Next.js Fast Refresh is automatic
- Disable unnecessary API calls in development
- Use browser DevTools for debugging
Next Steps
- Check Troubleshooting for common issues
- Explore the Architecture to understand the codebase
- Read Contributing Guidelines before making changes