React Native Config Variables
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.
Common errors
-
error: cannot find symbol class RNCConfigPackage
cause The `react-native-config` package's Android native module is not correctly linked or `MainApplication.java` is missing package registration.fixVerify 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`). -
Invariant Violation: `Config` has not been registered. This can happen if the `Config` module wasn't linked correctly.
cause The native module for `react-native-config` failed to load for iOS or Android, meaning the JavaScript bridge cannot find the `Config` object.fixFor 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. -
Undefined symbol: _OBJC_CLASS_$_RNCConfig
cause The `ReactNativeConfig.xcodeproj` or `libRNCConfig.a` is not correctly linked in Xcode, or `pod install` was not run after adding the package.fixIf 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. -
Property 'API_URL' does not exist on type 'Readonly<{}>'.cause TypeScript is not aware of the specific environment variables defined in your `.env` file, defaulting `Config` to an empty object type.fixCreate 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; }` -
A problem occurred evaluating project ':app'. > Project with path ':react-native-config' could not be found.
cause The `include ':react-native-config'` line or the `project(':react-native-config').projectDir` mapping in `android/settings.gradle` is incorrect or missing, preventing Gradle from locating the native module.fixVerify 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')`.
Warnings
- gotcha Secrets stored in `.env` files are not obfuscated or encrypted when packaged into a mobile app. Malicious users can reverse engineer apps to extract these values.
- breaking For React Native versions prior to 0.60, autolinking is not available. Manual linking steps for iOS, Android, and Windows are required, which can be complex and error-prone. Windows also requires manual linking for versions below 0.63.
- gotcha After installation and linking, Android projects require an additional manual step to apply a plugin in `android/app/build.gradle` for `react-native-config` to function correctly. Failure to do so will result in build errors or variables not being loaded.
- gotcha When using `react-native-config` in an iOS project that utilizes Cocoapods, you must run `pod install` in the `ios/` directory after adding the package to your `package.json` to ensure the native module is correctly linked.
- gotcha Projects upgrading to or using React Native's New Architecture (Fabric, TurboModules) on iOS or Android may encounter build issues if `react-native-config` isn't properly configured or if an incompatible version is used. Version 1.6.0+ introduced specific support for the new architecture.
Install
-
npm install react-native-config -
yarn add react-native-config -
pnpm add react-native-config
Imports
- Config
const Config = require('react-native-config');import Config from 'react-native-config';
Quickstart
import Config from "react-native-config";
import { useEffect, useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
const App = () => {
const [apiUrl, setApiUrl] = useState('');
const [googleMapsKey, setGoogleMapsKey] = useState('');
useEffect(() => {
// Variables from .env are automatically loaded into the Config object
// Ensure you have a .env file in your project root, e.g.:
// API_URL=https://myapi.com/v1
// GOOGLE_MAPS_API_KEY=your_google_maps_api_key
// SENSITIVE_KEY_EXAMPLE=DO_NOT_STORE_SECRETS_DIRECTLY_HERE
setApiUrl(Config.API_URL);
setGoogleMapsKey(Config.GOOGLE_MAPS_API_KEY);
}, []);
return (
<View style={styles.container}>
<Text style={styles.header}>React Native Config Example</Text>
<Text style={styles.label}>API URL:</Text>
<Text style={styles.value}>{apiUrl || 'Not set'}</Text>
<Text style={styles.label}>Google Maps Key:</Text>
<Text style={styles.value}>{googleMapsKey || 'Not set'}</Text>
<Text style={styles.warning}>
Warning: Do not store sensitive keys in .env for mobile apps as they are not obfuscated.
</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: '#f5f5f5',
},
header: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
color: '#333',
},
label: {
fontSize: 16,
fontWeight: '600',
marginTop: 10,
color: '#555',
},
value: {
fontSize: 16,
marginBottom: 5,
color: '#007bff',
},
warning: {
fontSize: 12,
color: 'red',
marginTop: 30,
textAlign: 'center',
},
});
export default App;