Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty config even though .env is filled up #143

Open
novito opened this issue Aug 7, 2017 · 67 comments
Open

Empty config even though .env is filled up #143

novito opened this issue Aug 7, 2017 · 67 comments

Comments

@novito
Copy link

novito commented Aug 7, 2017

I have created a react native app using create-react-native-app and I have this in my main app.js

import Config from 'react-native-config';

In my constructor I have:

console.log(Config.API_KEY)

And in my .env file I have:

API_KEY = test

That Config.API_KEY appears to be undefined.

Is this package supposed to work outside of Android and IOS (that is, straight on app.js)?

@rochapablo
Copy link

Same here.

.env

API_URL=http://

.js

import Config from 'react-native-config';
Config.API_URL // undefined

@rochapablo
Copy link

Problem fixed!

I haven't seen the Setup; Running react-native link react-native-config it worked.

@elyobo
Copy link

elyobo commented Aug 11, 2017 via email

@hhsadiq
Copy link

hhsadiq commented Aug 17, 2017

having the same issue. @elyobo how you fixed that?

@elyobo
Copy link

elyobo commented Aug 17, 2017

Carefully went through the instructions and checked that I'd done them all correctly. I realised that the react-native link react-native-config hadn't made all of the expected changes, possibly because my MainApplication.java was already heavily modified to integrate react-native-navigation. My recollection (possibly wrong) was that I had to manually go in and add new ReactNativeConfigPackage() to the last of packages returned in MainApplication::getPackages().

@GramosBegolli
Copy link

How are you creating .env file?. This was happening to me when I was creating it from new Text Document, this does not recognize the .env extension. You should create the .env file by terminal.

  • Go to project root directory cd Users/ProjectDir/
  • touch .env or e.x touch .env.dev
  • Edit that file with text editor such as Atom

I think this should be described on the Readme

@hhsadiq
Copy link

hhsadiq commented Aug 18, 2017

Thanks guys, I was able to fix this issue. There were multiple issues with my project. I was using sudo in all of my commands and it caused some strange issues in auto generated files. Wasted lots of times yesterday and today eventually had to scrape my repo and re-clone it and build it from scratch without sudos. Now it's working. Also, there were issues in linking. First, I was using this plugin without running the link command. Yesterday figured it out and ran link command but since there were strange sudo based issues going on, so it did not work well.

So to summarize, use react-native commands without sudo and do run link commands. Hopefully, things will work.

@isAlmogK
Copy link

I'm having the same issues, I followed all the steps for ios and it's still not working. I'm using WebStrom and running via terminal I have tried both react-native run-ios --scheme CCMobileApp Staging and react-native run-ios

@john1jan
Copy link

john1jan commented Aug 22, 2017

i am also facing the same issue. Config object is empty. Does anyone find any solution for it

@segalr3
Copy link

segalr3 commented Aug 22, 2017

i have the same issue after upgrading to RN 0.47.
tried upgrading to react-native-config to 0.5.0 without luck.

@jurby
Copy link

jurby commented Aug 23, 2017

+1 (react-native: 0.45.11, react: 16.0.0-alpha.12, react-native-config: ^0.6.0 / platform: android, development env: windows 10)

@john1jan
Copy link

I fixed it by unlinking and linking it again.

react-native unlink react-native-config
react-native link react-native-config

The only problem was i configured it wrongly before and linked it . And later even though fixing the configuration it was not working. Then unlinking and relinking worked

@isAlmogK
Copy link

Tried unlink and link getting the following error

ENOENT: no such file or directory, open '/Users/Almog/Full-Time-Employment/Construction-Cloud/RnD/CCMobileApp/android/app/src/main/java/com/ConstructionCloud/CCMobileApp/MainApplication.java'

@segalr3
Copy link

segalr3 commented Aug 23, 2017

Tried to re-link. Also tried version 0.6.0.
I get no errors, but i always get back an empty object for Config.
rolling back to RN 0.44 and react-native-config 0.3.1 fixes it.

@DavidKongDesheng
Copy link

Delete the app from your device or simulator, reinstall and fixed~

@isAlmogK
Copy link

@DavidKongDesheng that did not work, tried it twice. Everything works fine if you run it from Xcode but that kind of sucks prefer to run everything from terminal.

@amikofalvy
Copy link

I was able to get it working myself.
Make sure your env files are in the project root directory and not a subfolder.

Examine the ruby script:

  # find that above node_modules/react-native-config/ios/
  raw = File.read(File.join(Dir.pwd, "../../../#{file}"))

The location where react-native-config is looking for your .env* files is definitively in your project root directory.

Once I moved my config files to the correct location, I was able to access the config values.

Alternatively, for ios, if you want your files in a different directory you can modify the prebuild script to point to the directory you want to use.

I changed echo ".env.dev" > /tmp/envfile to echo "app/.env.dev" > /tmp/envfile so that I can put my config files in the project-root/app dir.

I hope this helps.

@cheesytim
Copy link

the same issue

@swl367
Copy link

swl367 commented Sep 20, 2017

I'm having the same issue with v0.6.1. I've ran all my linking correctly (I see the xcode project with the library, etc). However, my Config from my import is an empty object.

@andrejunges
Copy link

The only solution for me was to remove use_frameworks! from podfile.
After removing that line and reinstalling my pods it started to work as expected

@MattHall
Copy link

MattHall commented Oct 6, 2017

Just dropping a note here to hopefully save someone from doing the same thing I did:

Don't put any spaces between the variable name, the equals and the value. Doing this:

VARIABLE_NAME = 'somevalue'

Will cause the GeneratedInfoPlistDotEnv.h file to silently exclude the value. Be sure that your .env looks like this:

VARIABLE_NAME='somevalue'

@karlingen
Copy link

karlingen commented Oct 29, 2017

+1 cannot get it to work. I get the same problem

I have tried running the link command, different simulators and doing everything stated in the Setup section in the Readme.
I also deleted the app multiple times and re-installed it.
But nothing works.

@karlingen
Copy link

karlingen commented Oct 29, 2017

I managed to solve this by running these commands inside my project:

$ rm -rf $TMPDIR/react-* && watchman watch-del-all && rm -rf ios/build && rm -rf node_modules/ && npm cache clean --force && npm i

$ react-native link react-native-config

$ react-native run-ios

@acharlop
Copy link

Adding the following line to android/app/build.gradle fixed it for me
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

@jayehmke
Copy link

jayehmke commented Nov 5, 2017

I had the same issue, but @MattHall's solution fixed it for me.

@nlindroos
Copy link

An addition for (at least) React Native v0.50.4: react-native link did work for neither Android nor iOS. Android required the addition to android/app/build.gradle described by @acharlop. For iOS, ReactNativeConfig.xcodeproj needs to be manually added to Libraries and libReactNativeConfig.a added to Build Phases -> Link Binary With Libraries.

@RubenO88
Copy link

RubenO88 commented Dec 14, 2017

My problem was that react-native link worked, but I already had multiple targets in Xcode and the libReactNativeConfig.a was not linked in my development environment. Thanks @nlindroos for your comment above. Does anybody know how to add a new node package to a project and link it to both or possible even more targets?

@nlindroos
Copy link

@RubenO88 If react-native link doesn't do the necessary configuration automatically for a certain package, you can perform steps similar to the ones mentioned above, separately for each target. The required steps naturally differ between different packages.

@tmaly1980
Copy link

tmaly1980 commented Dec 30, 2017

Guys, I think this only works when you relaunch a project. ie making modifications to the .env file doesn't take effect until you run the build all over again. Relinking the native code might do it once, by coincidence, but what if you make changes, reload, make more changes, etc? I don't think it takes effect on-the-fly.

@rochapablo
Copy link

@tmaly1980, is right! You'll need re-build the project every time that your .env changes.

@andreleon
Copy link

andreleon commented Jun 28, 2018

On iOS I managed to fix this by changing the order of Linked Frameworks dan Libraries. Just drag libReactNativeConfig.a above libReact.a, recompile your project and it works.

screen shot 2018-06-28 at 10 29 43

@ferlopezm94
Copy link

@toeyanuntachai
Copy link

@elyobo Thank a lot, I've manually added new ReactNativeConfigPackage().
it's worked

@stepankuzmin
Copy link

Same here.

"react": "16.6.0",
"react-native": "0.57.4",
"react-native-config": "0.11.5"

@luongs3
Copy link

luongs3 commented Oct 29, 2018

each time i install this package, this issue shows up.
And i still don't know how to resolve it =))
Please, anyone ...

@RongGOHIN
Copy link

@luongs3 https://github.com/luggit/react-native-config#setup

@pacozaa
Copy link

pacozaa commented Dec 15, 2018

Yes we should add new ReactNativeConfigPackage() but still doesn't solve the problem for me.

@pacozaa
Copy link

pacozaa commented Dec 15, 2018

@bericp1 solotuion works for me.
To conclude the solution

  1. add new ReactNativeConfigPackage()
  2. read advance android setting and add as mentioned in document
defaultConfig {
    ...
    resValue "string", "build_config_package", "YOUR_PACKAGE_NAME_IN_ANDROIDMANIFEST.XML"
}
 

@tmaly1980
Copy link

I've dug into the module a bit more and how it works is the native code (Swift/Java) generates header files based on the contents of .env. Changes will not show up until the project is cleaned and re-built. The javascript code simply loads the constants defined in the native level code.

@jamessqr
Copy link

@nlindroos solution is still working and worked for me. I'm using React Native 0.57. I had to manually add the linked library in xcode and everything started working. This, despite the fact that react-native link command all reported back successful.

@slipkinem
Copy link

log: Could not find BuildConfig class.
gradle version: 3.3.1
give up usage.

@sudiddo
Copy link

sudiddo commented May 20, 2019

i manage to solve this issue by removing semicolon in the package json

this is the script for package.json in this repo's readme

ENVFILE=".env.staging"; react-native run-ios

this is my package.json script

"android": "ENVFILE=.env.dev react-native run-android"

i hope this help someone

@legshampoo
Copy link

I had this problem trying to use build variants for Android.

Solved it by doing this in MainApplication.java
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;
and further down
new ReactNativeConfigPackage()

and then following the 'advanced android setup' step in the readme, but modifying it to be
defaultConfig { resValue "string", "build_config_package", "${applicationId}" }
where the ${applicationId} was the deciding factor in my case, because I was adding an 'applicationIdSuffix' to each build version

@The0racle
Copy link

If you're using multiple flavors on Android, with different applicationId (not applicationIdSuffix), do what @loginov-rocks and @bericp1 said as it should fix.

@conor909
Copy link

conor909 commented Aug 20, 2019

@legshampoo I always get this:
ERROR: Project with path ':react-native-config' could not be found in project ':app'.

Is there a linking step for Android?
Works fine for iOS

@Chepkeitany
Copy link

Still getting the same issue. I get an empty object for Config. Works perfectly in the native code though only the Javascript part that's problematic.

@jeveloper
Copy link

@Chepkeitany HI there , i had noticed that the lastest release builds (no longer apk but aab due to new google requirements) with no other changes is turning Config into empty object. Builds are not failing.

Have you or others seen this?
RN 0.59
node 12
macos

single .env file

@Chepkeitany
Copy link

@jeveloper That's a similar experience. Although I didn't take note when Config was being returned as an empty object. Now that I think about it, I think it was around that time when we switched to aab on the playstore.

@conor909
Copy link

conor909 commented Aug 27, 2019

I got it working for Android eventually, here's my setup. (iOS just worked without any manual steps for me)

"react-native": "0.59.0"
"react-native-config": "0.11.5"

I'm also using yarn workspaces with the .env file outside the react-native project root, so to start android I need to give the path to the start command in my package.json:

scripts: {
   "start-android-dev": "ENVFILE=../../.env react-native run-android",
}

> android/settings.gradle

rootProject.name = 'MY-PROJECT-NAME'
...
include ':react-native-config'
project(':react-native-config').projectDir = new File(rootProject.projectDir, '/MY-PATH-TO/node_modules/react-native-config/android')
...
include ':app'

> android/app/build.gradle

apply plugin: "com.android.application"
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
...
...
project.ext.envConfigFiles = [
  debug : ".env",
  release : ".env",
  stagingrelease : ".env.staging",
  productionrelease : ".env.production"
]
...
...
dependencies {
    ...
    implementation project(':react-native-config')
    ...
}

> android/app/main/src/java/MY-APP-NAME/MainApplication.java

...
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;
import com.MY-APP-NAME.BuildConfig;
...
...
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
       ...
        new ReactNativeConfigPackage()
       ...
      );
    }

@helderberto
Copy link

Maybe in issue #400 I made a response that could help you.
See here.

@chenop
Copy link

chenop commented Mar 10, 2020

Note that "PACKAGE_NAME_IN_ANDROID_MANIFEST.XML" means:
[Package name in AndroidManifest.xml]
not
[package name in AndroidManifest].xml

defaultConfig {
    resValue "string", "build_config_package", "com.packageName" // No .xml extension!
   ...
}

Yes I felt for it...

@TNChalise
Copy link

Mine was space at the beginning of the .env file.

@rubentlc
Copy link

In my case, what fixed the problem was this point: https://github.com/luggit/react-native-config#problems-with-proguard

@AizenSousuke
Copy link

is there a fix for this issue at this point of time? doesn't seem to work in js

@dylanhillier
Copy link

I had some issues recently on iOS where changes in my .env file were not reflected in my mobile app.
What didn't work:

  • deleting the ios/build directory and rebuilding.
  • running pod install again, then rebuilding.
  • clearing metro cache and rebuilding.
  • deleting the app from the simulator and rebuilding + reinstall.

What did work:

  • Close the simulator + close metro + rebuild app.

It's not entirely clear whether or not the need to close the simulator is a result of some misconfiguration on my end.
I'm using react-native-config@1.5.0.

@owen800q
Copy link

@dylanhillier
Working for me, I think the doc need to mention every time anything inside .env has been changed, need to rebuild the app.

@dylanhillier
Copy link

dylanhillier commented Mar 12, 2023

@owen800q

I use the scripts from within package.json typically to build/run my react-native mobile app.

On Android, if my mobile app is running via a simulator, and I make a change to the .env file, then for this change to be make available to my app, i just need to re-run yarn android. I consider this to be working as expected.

On iOS, if my mobile app is running via a simulator, and i make a change to the .env file, then this change will not be visible to my mobile app by simply running yarn ios. I consider this to be not working as expected. For my .env changes to be available, I have to run the build via xcode.

This is inconsistent and undesirable. It would be nice for it to just work via yarn ios and to be consistent between ios and android.

edit:
This appears to be a known and ongoing issue. The following issues are likely due to the same root-cause.

  1. XCode does not pick up dotenv #703
  2. iOS bug #737
  3. New v1.5 has issues with CodePush #731
  4. GeneratedInfoPlistDotEnv.h not updated on cmd-builds (iOS) #710
  5. ENVFILE=.env.xxx has no effect on iOS unless also touching BuildDotenvConfig.rb #698

@Biplovkumar
Copy link

Same issue in 1.5.1.

@ttruongatl
Copy link

In the code snippet from the RNCConfigModule.java, the package name (stored in the variable className, though the name might be a bit misleading) is intended to be retrieved from strings.xml using a specific method. However, if the build_config_package string is absent in strings.xml, the system defaults to using the ApplicationId from build.gradle through the method getReactApplicationContext().getApplicationContext().getPackageName().

This creates an issue when the application Id is different from the packageName defined in MainApplication.java.

To resolve this issue, you should add a new line in the strings.xml file:

<string name="build_config_package">your_package_name</string>

Here, your_package_name should match the package name defined in your MainApplication.java file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests