|
| 1 | +# Environment Variables Configuration |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This React application uses Vite environment variables for configuration. All client-side environment variables must be prefixed with `VITE_` to be accessible in the browser. |
| 6 | + |
| 7 | +## File Structure |
| 8 | + |
| 9 | +- `.env.development` - Development environment variables |
| 10 | +- `.env.production` - Production environment variables (template only) |
| 11 | +- `.env.example` - Template file for new developers |
| 12 | +- `.env.local` - Local overrides (not committed to git) |
| 13 | + |
| 14 | +## Required Variables |
| 15 | + |
| 16 | +### API Configuration |
| 17 | + |
| 18 | +```bash |
| 19 | +VITE_API_BASE_URL=http://localhost:5000/api # Development |
| 20 | +VITE_API_BASE_URL=https://your-api-domain.com/api # Production |
| 21 | +``` |
| 22 | + |
| 23 | +## Vercel Deployment Setup |
| 24 | + |
| 25 | +### Method 1: Vercel Dashboard (Recommended) |
| 26 | + |
| 27 | +1. Go to your Vercel project dashboard |
| 28 | +2. Navigate to **Settings** → **Environment Variables** |
| 29 | +3. Add the following variables: |
| 30 | + - **Name**: `VITE_API_BASE_URL` |
| 31 | + - **Value**: `https://your-api-domain.com/api` |
| 32 | + - **Environment**: Production (or specify environment) |
| 33 | + |
| 34 | +### Method 2: Vercel CLI |
| 35 | + |
| 36 | +```bash |
| 37 | +vercel env add VITE_API_BASE_URL |
| 38 | +# Enter the value when prompted |
| 39 | +``` |
| 40 | + |
| 41 | +### Method 3: vercel.json Configuration |
| 42 | + |
| 43 | +Add to vercel.json: |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "env": { |
| 48 | + "VITE_API_BASE_URL": "https://your-api-domain.com/api" |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Usage in Code |
| 54 | + |
| 55 | +```typescript |
| 56 | +// Access environment variables in your React components |
| 57 | +const apiBaseUrl = import.meta.env.VITE_API_BASE_URL; |
| 58 | + |
| 59 | +// Check if in production |
| 60 | +const isProduction = import.meta.env.PROD; |
| 61 | + |
| 62 | +// Check if in development |
| 63 | +const isDevelopment = import.meta.env.DEV; |
| 64 | +``` |
| 65 | + |
| 66 | +## Security Notes |
| 67 | + |
| 68 | +- ⚠️ **Client-side variables are public** - Never store secrets in VITE\_ variables |
| 69 | +- ✅ Use VITE\_ prefix for public configuration only |
| 70 | +- ✅ API keys and secrets should be handled by your backend API |
| 71 | +- ✅ Always validate environment variables in your code |
| 72 | + |
| 73 | +## Local Development |
| 74 | + |
| 75 | +1. Copy `.env.example` to `.env.local` |
| 76 | +2. Update the values for your local setup |
| 77 | +3. The app will automatically use these variables |
| 78 | + |
| 79 | +## Troubleshooting |
| 80 | + |
| 81 | +- Variables not loading? Check the `VITE_` prefix |
| 82 | +- Build issues? Ensure variables are set in Vercel dashboard |
| 83 | +- Local development issues? Check `.env.local` file exists and has correct values |
0 commit comments