lugg / react-native-config

Bring some 12 factor love to your mobile apps!
MIT License
4.78k stars 656 forks source link

Empty config even though `.env` is filled up #143

Open novito opened 6 years ago

novito commented 6 years ago

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 commented 6 years ago

Same here.

.env

API_URL=http://

.js

import Config from 'react-native-config';
Config.API_URL // undefined
rochapablo commented 6 years ago

Problem fixed!

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

elyobo commented 6 years ago

react-native link react-native-config didn't do everything for me, but making the changes manually sorted it out

On 11 August 2017 at 21:59, Pablo notifications@github.com wrote:

Problem fixed!

I haven't seen the Setup https://github.com/luggit/react-native-config#setup; Running react-native link react-native-config it worked.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/luggit/react-native-config/issues/143#issuecomment-321794990, or mute the thread https://github.com/notifications/unsubscribe-auth/AAY4_pIqFseOTk8xou1Qmfto3mCPaQMtks5sXEIqgaJpZM4Ov9nh .

hhsadiq commented 6 years ago

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

elyobo commented 6 years ago

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 commented 6 years ago

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.

I think this should be described on the Readme

hhsadiq commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

segalr3 commented 6 years ago

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

jurby commented 6 years ago

+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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

isAlmogK commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

the same issue

swl367 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

+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 commented 6 years ago

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 commented 6 years ago

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

jayehmke commented 6 years ago

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

nlindroos commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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

zhangqiangoffice commented 6 years ago

@MattHall ,thank you ! You saved my APP!

loginov-rocks commented 6 years ago

@elyobo thank you a lot! react-native link tried to use MainApplication.java that doesn't belongs to the project, so I've manually added new ReactNativeConfigPackage() like you mentioned. Just wanted to add that it shold be imported with import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

bericp1 commented 6 years ago

For people running into this issue on Android using either applicationIdSuffix or build flavors, this is also a symptom of not properly setting the build_config_package resValue to your base package name (i.e. without the suffix) like the "Advanced Android Setup" section in the readme describes. See also #15.

This is documented but may be hard to catch since the error message (ReactNative: ReactConfig: Could not find BuildConfig class logged in android/src/main/java/com/lugg/ReactNativeConfig/ReactNativeConfigModule.java) only shows up in logcat and doesn't explicitly cause a crash.

BobAleena commented 6 years ago

I'm seeing this issue when running npm test. Using react-native-config works when running my app though. I have a .env file in the root dir of the RN app which works. When running my test, it doesn't seem to find the .env file thus all the variables are undefined. I am assuming it is a matter of either creating another .env file for the tests specifically (but I don't know where to actually put that), or somehow specify the original .env file (also not sure how to do that).

Tarpsvo commented 6 years ago

Same issue for me. Works on iOS, but getting BuildConfig class not found error on Android.

D/ReactNative: ReactConfig: Could not find BuildConfig class

I have confirmed that the package name matches the applicationId, no suffix is being used. Also tried still manually adding the proper build_config_package resValue to no avail.

rhogeranacleto commented 6 years ago

Thanks @AlmogRnD, thats worked to me!

liyanxi commented 6 years ago

@rhogeranacleto I still have problems, How did you solve it? android release apk: enabled minifyEnabled true D/ReactNative: ReactConfig: Could not find BuildConfig class

Tarpsvo commented 6 years ago

I found out that react-native run-iosworks for iOS, but if you want Android to use the scheme, you had to build it using Android Studio, as react-native run-android did not manage to build the env file into BuildConfig for some reason. Mysteries. Eventually gave up on react-native.

andreleon commented 6 years ago

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 commented 6 years ago

This tutorial helped me: https://medium.com/differential/managing-configuration-in-react-native-cd2dfb5e6f7b.

toeyanuntachai commented 5 years ago

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

stepankuzmin commented 5 years ago

Same here.

"react": "16.6.0",
"react-native": "0.57.4",
"react-native-config": "0.11.5"
luongs3 commented 5 years ago

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

RongGOHIN commented 5 years ago

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

pacozaa commented 5 years ago

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

pacozaa commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

sudiddo commented 5 years ago

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