2

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

For me using the following versions it worked on iOS after I ran pod install inside ios folder. For android all I had to do was the extra step that is mentioned in repo's readme.md: https://github.com/lugg/react-native-config?tab=readme-ov-file#extra-step-for-android

    "react-native": "0.73.6",
    "react-native-config": "^1.5.3",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.