.env files, making configuration management seamless across development, testing, and production environments.
Quick Start
Create a.env file in your project root:
.env
Access environment variables
.env automatically!
.env File Priority
Bun loads multiple.env files in priority order:
1
Process Environment
Variables already set in the shell (highest priority)
2
.env.local
Local overrides (gitignored by convention)
3
.env.[environment].local
Environment-specific local overrides:
.env.development.local.env.production.local.env.test.local
4
.env.[environment]
Environment-specific configuration:
.env.development.env.production.env.test
5
.env
Base configuration (lowest priority)
NODE_ENV=production→ loads.env.productionNODE_ENV=development→ loads.env.development(default)NODE_ENV=test→ loads.env.test
src/env_loader.zig:1-18 - .env file detection
.env File Format
Standard.env syntax:
.env syntax
Escaping
Escaping special characters
Accessing Variables
process.env
Node.js-compatible API:process.env
Bun.env
Bun-specific API (read-only):Bun.env
Bun.envis read-onlyprocess.envallows runtime modifications- Both reference the same underlying data
Custom .env Files
Load specific .env files:CLI Flag
Custom env file
bunfig.toml
bunfig.toml
src/bunfig.zig:151-186 - Env config parsing
Programmatic Loading
Manual loading
Environment-Specific Configuration
Development
.env.development
Production
.env.production
Testing
.env.test
Local Overrides
.env.local (gitignored)
.gitignore:
.gitignore
Type Safety
Declare Types
env.d.ts
Validate Variables
Validation
Zod Schema
Zod validation
Special Variables
NODE_ENV
Determines which.env.[environment] file loads:
NODE_ENV
PATH
System executable search paths:PATH
HOME / USERPROFILE
User home directory:Home directory
Proxy Variables
HTTP/HTTPS proxy configuration:Proxy variables
src/env_loader.zig:167-198 - HTTP proxy detection
TLS Configuration
TLS variables
src/env_loader.zig:144-161 - TLS reject unauthorized
Security Best Practices
1. Gitignore .env Files
.gitignore
2. Use .env.example
Commit a template without secrets:.env.example
3. Validate Required Variables
Startup validation
4. Restrict Permissions
File permissions
5. Use Secrets Management
For production, use proper secrets management:- AWS Secrets Manager
- HashiCorp Vault
- Azure Key Vault
- Google Secret Manager
Fetch from secrets manager
Environment Loading Implementation
Bun’s environment variable loading:Loader structure (src/env_loader.zig:7-29)
- Check each environment file
- Parse key=value pairs
- Expand variables (
${VAR}) - Merge into environment (lower priority files don’t override)
Advanced Usage
Environment Detection
Environment detection
src/env_loader.zig:70-76 - CI detection
Dynamic Configuration
Dynamic config
Variable Expansion
Variable expansion
Debugging
Print All Variables
Print environment
Check Variable Loading
Debug env loading
Verify .env Parsing
Test .env file
Troubleshooting
Variables Not Loading
- Check file location:
.envmust be in project root - Check file name: Must be exactly
.env(no spaces) - Check syntax:
KEY=value(no spaces around=) - Check NODE_ENV: Correct environment file loaded?
Variables Undefined
Check if loaded
Priority Issues
Check which file sets the variable:Check all .env files
Related
- bunfig.toml - Configure env loading behavior
- Runtime Overview - Runtime configuration