Environment Variables
Every Basecompose-generated project includes environment variables for configuration. This guide explains what each variable does and how to set them.
Overview
Environment variables are settings that configure your application without changing code. Every generated project includes a .env.example file showing all available variables.
Two Files
.env.example- Template with all possible variables (committed to git).env.local- Your actual values (NOT committed to git)
Setup
cp .env.example .env.local
# Edit .env.local with your valuesMongoDB Variables
Included if you select MongoDB in your stack.
Development
# Connection string for MongoDB
MONGODB_URI=mongodb://root:example@localhost:27017/basecompose
# Authentication credentials
MONGODB_ROOT_USERNAME=root
MONGODB_ROOT_PASSWORD=exampleProduction
# Use full connection string with credentials
MONGODB_URI=mongodb://root:securepass@mongo.example.com:27017/basecompose?ssl=true
# Strong credentials
MONGODB_ROOT_USERNAME=root
MONGODB_ROOT_PASSWORD=<generate_strong_password>How It Works
The connection string format:
mongodb://[username]:[password]@[host]:[port]/[database]Example breakdown:
mongodb://root:example@localhost:27017/basecompose
├─ mongodb:// - Protocol
├─ root - Username
├─ example - Password
├─ localhost - Host
├─ 27017 - Port
└─ basecompose - Database nameAuthentication (Auth.js) Variables
Included if you select Auth.js in your stack. Requires setting up OAuth applications.
GitHub OAuth
GITHUB_ID=your_github_app_id
GITHUB_SECRET=your_github_app_secretHow to get:
- Go to GitHub Settings → Developer Settings → OAuth Apps → New OAuth App
- Fill in:
- Application name: "Basecompose Local"
- Homepage URL:
http://localhost:3000 - Authorization callback URL:
http://localhost:3000/api/auth/callback/github
- GitHub provides the ID and secret
- Copy them to
.env.local
Google OAuth
GOOGLE_CLIENT_ID=your_google_client_id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your_google_client_secretHow to get:
- Go to Google Cloud Console
- Create a new project
- Enable Google+ API
- Create OAuth 2.0 credentials (Web Application)
- Add authorized redirect URIs:
http://localhost:3000/api/auth/callback/google
- Copy Client ID and Client Secret
- Add to
.env.local
NextAuth Configuration
# Your application URL
NEXTAUTH_URL=http://localhost:3000
# Secret for signing and encrypting tokens
NEXTAUTH_SECRET=<generate_random_secret>Generating NEXTAUTH_SECRET:
# On Mac/Linux
openssl rand -base64 32
# On Windows (using Node.js)
node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"Production URL:
# Change for production deployment
NEXTAUTH_URL=https://yourdomain.comNext.js Variables
Standard Next.js configuration:
# Environment mode
NODE_ENV=development
# or
NODE_ENV=productionAvailable Values
development- Dev server with hot reload and detailed errorsproduction- Optimized builds and error loggingtest- For running tests
Docker & Service Variables
When running with Docker Compose, services communicate via hostnames:
# In development (Docker)
MONGODB_URI=mongodb://mongodb:27017/basecompose
# In development (Local)
MONGODB_URI=mongodb://localhost:27017/basecompose
# In production
MONGODB_URI=mongodb://[remote-host]:27017/basecomposeExample .env.local Files
Minimal Setup
MONGODB_URI=mongodb://root:example@mongodb:27017/basecompose
MONGODB_ROOT_USERNAME=root
MONGODB_ROOT_PASSWORD=example
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your-random-secret-here
GITHUB_ID=your_github_id
GITHUB_SECRET=your_github_secret
NODE_ENV=developmentProduction Setup
MONGODB_URI=mongodb://user:securepass@mongo.example.com:27017/prod_db
MONGODB_ROOT_USERNAME=prod_user
MONGODB_ROOT_PASSWORD=<strong-password>
NEXTAUTH_URL=https://myapp.com
NEXTAUTH_SECRET=<generated-secure-secret>
GITHUB_ID=production_github_id
GITHUB_SECRET=production_github_secret
NODE_ENV=productionLoading Variables
Development (Local)
Next.js automatically loads .env.local:
pnpm dev
# Reads .env.local automaticallyAccess in code:
const mongoUri = process.env.MONGODB_URI;Production (Docker)
Set via Docker environment:
docker run \
-e MONGODB_URI="mongodb://..." \
-e NEXTAUTH_SECRET="secret" \
your-imageOr with Docker Compose:
services:
app:
environment:
MONGODB_URI: ${MONGODB_URI}
NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}Environment File (Docker)
# Create .env file
docker-compose --env-file .env upSecurity Best Practices
- Never Commit
.env.local- Add to.gitignore(already done) - Use Strong Passwords - Especially for production
- Rotate Secrets - Regularly update API keys and tokens
- Use Secrets Manager - For production (AWS Secrets Manager, etc.)
- Different Values Per Environment - Dev, staging, and production should have different values
- Validate on Startup - Check required variables exist
Validating Variables
Add this to your app to catch missing variables early:
// lib/env.ts
function requireEnv(key: string): string {
const value = process.env[key];
if (!value) {
throw new Error(`Missing required environment variable: ${key}`);
}
return value;
}
// Use it
export const MONGODB_URI = requireEnv('MONGODB_URI');
export const NEXTAUTH_SECRET = requireEnv('NEXTAUTH_SECRET');Troubleshooting
Variables Not Loading
Error: Cannot read property 'MONGODB_URI' of undefinedSolution:
- Check
.env.localexists - Restart dev server:
Ctrl+Cthenpnpm dev - Verify file has correct format:
KEY=value
Wrong Values in Production
Solution:
- Verify environment variables in Docker container:bash
docker exec <container> env | grep MONGODB - Check Docker Compose or deployment configuration
For more on deployment, see Getting Started.