Registry / devops / react-native-build-config

react-native-build-config

JSON →
library0.3.2jsnpmunverified

react-native-build-config is a utility module for React Native that exposes native build configuration variables (from Gradle for Android and Info.plist for iOS) directly to your JavaScript codebase. This allows developers to manage environment-specific settings, API endpoints, application version details (like `VERSION_NAME` and `VERSION_CODE`), and debug flags consistently across both native and JavaScript layers of their application. The current stable version is 0.3.2, with releases typically tied to compatibility updates for new React Native versions or addressing platform-specific nuances. Its primary differentiation is its direct integration with existing native build systems, avoiding the need for separate JavaScript-specific environment variable solutions and ensuring synchronization with native build variants.

npm install react-native-build-config
INSTALL
IMPORT
SIG · REACT-NATIVE-BUILD
R
react-native-build-config
devopsjavascriptv0.3.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BuildConfig
✓ import BuildConfig from 'react-native-build-config';
✗ import { BuildConfig } from 'react-native-build-config';
The library primarily exports a default object containing the build configuration. While CommonJS `require` also works, ESM import should use the default import pattern.
BuildConfig (CJS)
✓ const BuildConfig = require('react-native-build-config');
This is the documented CommonJS import method, widely compatible with older Node.js environments and React Native versions.

This example demonstrates how to access various build configuration variables, including custom ones like `API_URL` and `SHOW_ERRORS`, as well as standard native build variables like `VERSION_NAME` and `DEBUG`, from the `BuildConfig` object within a React Native functional component.

import BuildConfig from 'react-native-build-config'; import { useEffect, useState } from 'react'; import { Text, View, StyleSheet } from 'react-native'; const App = () => { const [config, setConfig] = useState({}); useEffect(() => { // In a real app, these would be configured in build.gradle (Android) // or Info.plist (iOS) during the native build process. // Example of accessing pre-defined config: setConfig({ apiUrl: BuildConfig.API_URL || 'https://defaultapi.com', showErrors: BuildConfig.SHOW_ERRORS || false, versionName: BuildConfig.VERSION_NAME || '1.0.0', versionCode: BuildConfig.VERSION_CODE || 1, applicationId: BuildConfig.APPLICATION_ID || 'com.example', debug: BuildConfig.DEBUG || false, buildType: BuildConfig.BUILD_TYPE || 'release', }); }, []); return ( <View style={styles.container}> <Text style={styles.title}>App Configuration:</Text> {Object.entries(config).map(([key, value]) => ( <Text key={key} style={styles.item}> {key}: {String(value)} </Text> ))} <Text style={styles.note}> (Values are sourced from native build config like build.gradle or Info.plist) </Text> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'flex-start', padding: 20, backgroundColor: '#f5f5f5', }, title: { fontSize: 22, fontWeight: 'bold', marginBottom: 15, color: '#333', }, item: { fontSize: 16, marginBottom: 5, color: '#555', }, note: { fontSize: 12, marginTop: 20, color: '#888', fontStyle: 'italic' } }); export default App;
Debug
Known issues
breakingFor React Native versions prior to 0.60, auto-linking is not supported. You must manually link the native module for both Android and iOS projects to ensure `react-native-build-config` functions correctly. Attempting to use versions > 0.1.0 without manual linking on older RN versions will result in runtime errors.
fix
For React Native < 0.60, use `react-native-build-config@0.1.0` and follow the manual linking instructions provided in the package's README, which involves modifying `android/settings.gradle`, `android/app/build.gradle`, and `MainActivity.java` (for Android).
affects: <0.60.0
gotchaThe method for defining configuration variables differs between Android and iOS. Android requires `buildConfigField` declarations in `android/app/build.gradle`, while iOS uses `<key>` and `<value>` entries within the `Info.plist` file. Misconfiguration on either platform will lead to variables not being exposed in JavaScript.
fix
Ensure variables are correctly declared in `android/app/build.gradle` (using `buildConfigField`) for Android builds and in `ios/YourApp/Info.plist` (using `<key>` and `<string>`/`<true>`/`<false>`) for iOS builds. Variable names must match exactly between native declarations and JS access.
affects: >=0.1.0
gotchaSome native build variables (like `VERSION_CODE`) are exposed with specific types. For example, `VERSION_CODE` is a number in native, and exposed as such, while `VERSION_NAME` is a string. `DEBUG` is a boolean. Developers should be mindful of these types when accessing variables in JavaScript to avoid unexpected type-related errors.
fix
Always anticipate the native type for known variables (e.g., `VERSION_CODE` as a number, `DEBUG` as a boolean) and perform type checks or explicit conversions in JavaScript if strict type handling is required.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read property 'API_URL' of undefined
The native module `RNBuildConfig` was not correctly linked or initialized, or the `BuildConfig` object is not available at runtime.
fix
Ensure that `react-native-build-config` is properly linked. If React Native < 0.60, manual linking is required. If RN >= 0.60, confirm auto-linking is working (e.g., by running `npx react-native run-android` or `npx react-native run-ios` to rebuild native modules). Also verify that the native build config (Gradle/Info.plist) has been properly configured and rebuilt.
Error: buildConfigField "String", "API_URL", "\"https://myapi.com\"" does not define a valid field for BuildConfig.
This error typically occurs during the Android build process because `buildConfigField` is used outside of the `defaultConfig` or `buildTypes` blocks in `android/app/build.gradle`, or has incorrect syntax.
fix
Place `buildConfigField` declarations within the `defaultConfig { ... }` block or within specific `buildTypes { debug { ... } }` / `productFlavors { ... }` blocks in your `android/app/build.gradle` file. Double-check the syntax, ensuring correct type, name, and string-wrapped value format.
Upgrade
Version history
0.3.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
react-native-build-config — npm install react-native-build-config · libregistry