@nestjs/config module configured globally in app.module.ts:13.
Quick Start
Create a.env file in the root directory of your project:
Required Variables
DATABASE_URL
PostgreSQL connection string used by Prisma for database access.username: PostgreSQL userpassword: User passwordhost: Database server hostname or IPport: PostgreSQL port (default: 5432)database: Database nameschema: PostgreSQL schema (default: public)
prisma.config.ts:10- Prisma configurationprisma/schema.prisma:5-7- Datasource configuration
The connection string must point to a PostgreSQL database. Other database providers are not currently supported.
JWT_SECRET
Secret key for signing and verifying JWT tokens used in authentication.- Use at least 32 characters
- Generate using a cryptographically secure method
- Use different secrets for each environment
- Rotate periodically in production
src/modules/auth/auth.module.ts:13- JWT module configurationsrc/common/jwt.strategy.ts:16- JWT strategy validation
Optional Variables
PORT
The port on which the application server listens.3000 (configured in src/main.ts:17)
Valid range: 1024-65535
Common ports:
- Development:
3000 - Production:
8080,3000, or platform-specific
SHADOW_DATABASE_URL
Optional shadow database URL used by Prisma Migrate for migration development.prisma.config.ts:11- Prisma migration configuration
- Development environments with migration creation
- CI/CD pipelines running migrations
- When you don’t have permission to create temporary databases
The shadow database should be a separate database with the same schema permissions as your main database.
Environment-Specific Configuration
Development
Create.env.development.local for development overrides:
Production
Set environment variables through your hosting platform:Validation Pipeline
The application usesclass-validator with a global validation pipe configured in src/main.ts:9-15:
Configuration Module
TheConfigModule is imported globally in src/app.module.ts:13-15:
ConfigService anywhere in the application:
Loading Priority
Environment variables are loaded in the following order (later sources override earlier ones):- System environment variables
.envfile (base configuration).env.local(local overrides, gitignored).env.development.localor.env.production.local(environment-specific)- Command-line environment variables
Security Best Practices
Use secret management in production
Use secret management in production
Never store secrets in
.env files in production. Use platform-provided secret management:- AWS: Secrets Manager or Parameter Store
- Azure: Key Vault
- Google Cloud: Secret Manager
- Docker: Docker Secrets
Rotate secrets regularly
Rotate secrets regularly
Implement a secret rotation policy:
- JWT secrets: Every 90 days
- Database passwords: Every 60-90 days
- API keys: Based on provider recommendations
Use different secrets per environment
Use different secrets per environment
Never reuse secrets across environments:
- Development secrets should differ from production
- Each deployment environment gets unique credentials
- Leaked development secrets don’t compromise production
Verify .gitignore configuration
Verify .gitignore configuration
The Never commit changes that expose these files.
.gitignore file already excludes:Troubleshooting
Database connection fails
Database connection fails
Error:
Can't reach database serverSolutions:- Verify
DATABASE_URLformat is correct - Check database server is running
- Confirm network access to database host
- Verify credentials are correct
- Check PostgreSQL port (default 5432) is accessible
JWT authentication fails
JWT authentication fails
Error:
Unauthorized or Invalid tokenSolutions:- Verify
JWT_SECRETmatches between environments - Check token hasn’t expired (default: 3 hours)
- Ensure secret is set (not using fallback
'supersecret') - Verify Authorization header format:
Bearer <token>
Environment variables not loading
Environment variables not loading
Solutions:
- Ensure
.envfile is in project root - Restart the application after changing
.env - Check for syntax errors in
.envfile - Verify
@nestjs/configis installed - Confirm
ConfigModule.forRoot()is imported inAppModule
Port already in use
Port already in use
Error:
EADDRINUSE: address already in use :::3000Solutions:- Change
PORTin.envto different value - Kill process using the port:
lsof -ti:3000 | xargs kill - Use different port for parallel development environments
Next Steps
Deployment Guide
Learn how to deploy the application to production
Database Setup
Configure and migrate your PostgreSQL database