Environment Variables

React Native 6 min min read Updated: Aug 03, 2026 Intermediate
Environment Variables
Intermediate Topic 14 of 15

Environment Variables

Managing environment variables allows React Native applications to use different API URLs, keys, and configuration values across development, staging, and production environments.

Installing react-native-config

bash npm install react-native-config

Creating Environment Files

text .env .env.staging .env.production
text // .env.production API_URL=https://api.myapp.com SENTRY_DSN=https://xxxx@sentry.io/xxxx

Using Environment Variables in Code

javascript import Config from 'react-native-config'; const apiUrl = Config.API_URL;

Building with a Specific Environment

bash ENVFILE=.env.production npx react-native run-android

Never Commit Sensitive Values

Environment files containing secrets or API keys should always be added to .gitignore, with a template file (like .env.example) committed instead to document required variables.

text // .gitignore .env .env.staging .env.production

Managing Secrets in CI/CD

For automated builds, environment variables and secrets should be injected securely through your CI/CD platform’s encrypted secrets storage rather than committed environment files.

Type Safety with TypeScript

typescript declare module 'react-native-config' { export interface NativeConfig { API_URL: string; SENTRY_DSN: string; } const Config: NativeConfig; export default Config; }

Conclusion

Properly managing environment variables using react-native-config allows React Native applications to safely and cleanly switch between development, staging, and production configurations across different builds.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators