Types of Environment Variables
Expo supports two types of environment variables:- Build-time variables - Available during app configuration and bundling
- Runtime variables - Available in your app code at runtime
EXPO_PUBLIC Variables
Variables prefixed withEXPO_PUBLIC_ are embedded in your JavaScript bundle and available at runtime.
Setting EXPO_PUBLIC Variables
Create a.env file in your project root:
.env
Accessing at Runtime
Access these variables directly in your code:Important Notes
- Variables must be prefixed with
EXPO_PUBLIC_ - They are embedded in your JavaScript bundle
- They are visible in your published code
- Never use them for secrets or sensitive data
- Changes require restarting the dev server
Build-Time Variables
Variables without theEXPO_PUBLIC_ prefix are only available during configuration and bundling.
In App Configuration
Use inapp.config.js:
app.config.js
Accessing Build-Time Variables
Throughexpo-constants:
.env Files
Expo CLI automatically loads environment variables from.env files.
File Priority
Expo loads.env files in this order (later files override earlier):
.env- Default for all environments.env.local- Local overrides (git ignored).env.${ENV}- Environment-specific (e.g.,.env.production).env.${ENV}.local- Local environment overrides
Example Setup
Create multiple.env files:
.env
.env.development
.env.production
.env.local
Specifying Environment
Set theENV variable to load specific files:
.gitignore
Don’t commit sensitive files:.gitignore
.env.example
Setting Variables
Command Line
Set variables inline:Shell Configuration
Add to your shell profile (.bashrc, .zshrc):
Cross-Platform Scripts
Usecross-env for Windows compatibility:
Install:
package.json:
CI/CD Environments
GitHub Actions
Set environment variables in workflows:.github/workflows/build.yml
GitLab CI
.gitlab-ci.yml
EAS Build
Useeas.json:
eas.json
Expo CLI Environment Variables
Expo CLI itself uses environment variables for configuration:Common CLI Variables
boolean
Enable debug logging:
EXPO_DEBUG=1boolean
Skip network requests:
EXPO_OFFLINE=1boolean
Run in non-interactive CI mode:
CI=1boolean
Disable analytics:
EXPO_NO_TELEMETRY=1boolean
Disable API caches:
EXPO_NO_CACHE=1number
Metro bundler port:
PORT=8082Metro Configuration
boolean
Disable lazy bundling:
EXPO_NO_METRO_LAZY=1string
Path to custom Metro config:
EXPO_OVERRIDE_METRO_CONFIG=./metro.custom.jsDevelopment Features
string
Default editor to open:
EXPO_EDITOR=codeboolean
Enable bundle analysis:
EXPO_ATLAS=1string
Log debug events:
LOG_EVENTS=1 or LOG_EVENTS=events.logSecurity Best Practices
Never Commit Secrets
Don’t put sensitive data in:- Version control
- EXPO_PUBLIC variables
- Client-side code
Use Backend APIs
Store secrets on your backend:Validate on Server
Never trust client-provided values:Rotate Secrets
Regularly rotate API keys and tokens:- Generate new secrets
- Update environment variables
- Rebuild and deploy app
- Revoke old secrets
Common Patterns
Feature Flags
Control features with environment variables:Environment Detection
Detect current environment:API Configuration
Configure APIs per environment:Troubleshooting
Variables Not Available
If variables aren’t working:- Check prefix: Must start with
EXPO_PUBLIC_ - Restart dev server: Changes require restart
- Check .env file: Must be in project root
- Verify file name: Exact match (
.env, notenv.txt)
Variables Undefined at Runtime
Build-Time vs Runtime
Build-time variables won’t update without rebuilding:CI/CD Not Working
Ensure environment variables are set in CI:TypeScript Support
Add type definitions:env.d.ts