react-native-config is a module that provides a straightforward way to expose configuration variables to React Native applications, aligning with the 12 Factor App methodology for configuration. It supports multiple platforms including iOS, Android, macOS, and Windows. The current stable version is 1.6.1, with releases occurring frequently to address platform-specific issues, introduce new architecture support (like Fabric for iOS and Android), and extend compatibility to newer platforms like visionOS. Its key differentiators include broad platform support and a simple `.env` file interface, making it easy to manage environment-specific variables without modifying code. However, it's crucial to understand that it does not obfuscate or encrypt secrets, which is a fundamental limitation for mobile apps; therefore, sensitive keys should not be stored directly in `.env` files. The package emphasizes ease of use for non-sensitive configuration parameters across diverse mobile and desktop platforms supported by React Native.
npm install react-native-configVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define environment variables in a `.env` file and access them within a React Native component using `react-native-config`. It highlights the basic usage pattern for displaying configuration values and includes a crucial warning about storing sensitive information.
Design your backend APIs to not rely on client-side secrets, or use a secure vault/backend service for sensitive key management instead of storing them directly in the app's `.env`.
Refer to the `react-native-config` documentation for platform-specific manual linking instructions or upgrade your React Native project to a version that supports autolinking (RN 0.60+ for core, RN 0.63+ for Windows).
Add `apply from: project(':react-native-config').project` to the second line of your `android/app/build.gradle` file. Alternatively, use `npx react-native-integrate react-native-config`.Navigate to your `ios` directory (`cd ios`) and run `pod install`. Clean your build folder in Xcode afterwards (`Product > Clean Build Folder`).
Ensure you are on `react-native-config@1.6.0` or higher for New Architecture support. Follow the specific New Architecture setup steps provided in the official documentation, which often involves codegen and proper project configuration.
Verify manual linking steps for Android (`settings.gradle`, `app/build.gradle`, `MainApplication.java`) and ensure `apply from: project(':react-native-config').project` is in `android/app/build.gradle`. If using autolinking, ensure caches are cleared and rebuild (`./gradlew clean`).For iOS, run `cd ios && pod install` and clean the build folder (`Product > Clean Build Folder`) in Xcode. For Android, ensure all manual linking steps are followed, especially the `apply from` line in `app/build.gradle`, and then clean the Gradle cache (`./gradlew clean`). For older RN versions, ensure manual linking is complete as per documentation.
If using Cocoapods, navigate to `ios` and run `pod install`. If not using Cocoapods or for manual linking, ensure `ReactNativeConfig.xcodeproj` is added to `Libraries` and `libRNCConfig.a` is in `Build Phases` -> `Link Binary With Libraries` in your Xcode project. Clean Xcode build folder and derived data.
Create a declaration file (e.g., `src/types/env.d.ts` or `env.d.ts`) in your project root and declare the module with your environment variables. Example: `declare module 'react-native-config' { export interface NativeConfig { API_URL: string; GOOGLE_MAPS_API_KEY: string; [key: string]: string | undefined; } export const Config: NativeConfig; export default Config; }`Verify the `android/settings.gradle` file contains both `include ':react-native-config'` and the correct `project(':react-native-config').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-config/android')`.