Lesson 8 of 10
Environment Configuration - .env per Environment
Mobile apps typically run against three environments: development (local server), staging (QA server), and production. Environment variables configure which endpoint, log level, feature flags, and analytics keys to use.
Expo Environment Variables
# Expo SDK 49+ - built-in env var support
# Variables prefixed EXPO_PUBLIC_ are bundled and accessible in JS
# Variables WITHOUT EXPO_PUBLIC_ are build-time only (secrets)
# .env.development
EXPO_PUBLIC_API_URL=http://localhost:3000
EXPO_PUBLIC_ENV=development
EXPO_PUBLIC_SENTRY_DSN=https://abc@sentry.io/dev
EXPO_PUBLIC_FEATURE_DARK_MODE=true
# .env.staging
EXPO_PUBLIC_API_URL=https://staging-api.myapp.com
EXPO_PUBLIC_ENV=staging
EXPO_PUBLIC_SENTRY_DSN=https://abc@sentry.io/staging
# .env.production
EXPO_PUBLIC_API_URL=https://api.myapp.com
EXPO_PUBLIC_ENV=production
EXPO_PUBLIC_SENTRY_DSN=https://abc@sentry.io/prod
EXPO_PUBLIC_FEATURE_DARK_MODE=false
# ---- Access in code ----
// src/config/env.ts
export const ENV = {
apiUrl: process.env.EXPO_PUBLIC_API_URL!,
environment: (process.env.EXPO_PUBLIC_ENV ?? 'development') as 'development' | 'staging' | 'production',
sentryDsn: process.env.EXPO_PUBLIC_SENTRY_DSN,
features: {
darkMode: process.env.EXPO_PUBLIC_FEATURE_DARK_MODE === 'true',
},
} as const;
// Usage
import { ENV } from '@/config/env';
const apiClient = axios.create({ baseURL: ENV.apiUrl });
// ---- EAS build profiles (use different .env per build) ----
// eas.json
{
"build": {
"development": { "env": { "APP_ENV": "development" } },
"staging": { "env": { "APP_ENV": "staging" } },
"production": { "env": { "APP_ENV": "production" } }
}
}
# Load env file based on APP_ENV in app.config.ts:
import 'dotenv/config';
export default ({ config }: ConfigContext): ExpoConfig => {
const envFile = `.env.${process.env.APP_ENV ?? 'development'}`;
require('dotenv').config({ path: envFile });
return { ...config };
};Tip
Tip
Practice Environment Configuration env per Environment in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Managing separate API URLs, bundle IDs, and secrets across environments
Practice Task
Note
Practice Task - (1) Write a working example of Environment Configuration env per Environment from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Environment Configuration env per Environment is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?