react-native-config not working (Empty Config in both OS)

1.2k Views Asked by At

I'm really struggling with what seems like a very primitive error. I have installed react-native-config and when console.logging the Config object it returns {}.

The problem occurs both on android and iOS and it doesn't seem to be a linking issue because (even though RN 71.3 doesn't require manual linking) when following the steps to link manually on android I got an error saying RNCConfigPackage already linked.

This is a simplified version of my code:

import Config from 'react-native-config';

console.log(Config);

And this is what my .env file looks like:

REACT_APP_STAGE=development
TEST=ThisIsATest
ANOTHER_TEST=ThisIsAnotherTest

Debugging attempts

I have already tried:

  • Running gradlew clean and pod install (even though it's not a platform specific issue)
  • Run with ENVFILE=.env react-native run-android to hard code the env file

I understand that this is very limited information in order to debug the issue but any direction and help is appreciated including any approach you would follow to debug.

EDIT: The Config appeared in the end but is very late to update, an issue more people seem to be facing, I have used react-native-dotenv instead.

1

There are 1 best solutions below

0
Ali On

Here is a fix tested on

  • "react-native": "0.72.6"
  • "react-native-config": "^1.5.1"

Android

add to your app/build.gradle

project.ext.envConfigFiles = [
        debug: ".env",
        release: ".env",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

...

android {
...

    defaultConfig {
...
      resValue "string", "build_config_package", "com.yourApp.packageName"
    }

iOS

Go to Product > Scheme > Edit Scheme...

in the left panel Build > pre-actions add a new run script action and add the code below ( to generate the GeneratedDotEnv.m )

xcodebuild -project "${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig.xcodeproj"

then in your Pod file add this line under your target

pod 'react-native-config', :path => '../node_modules/react-native-config'

and last step : cd ios && pod install