react-native-dotenv: APP_ENV doesn't work as expected
I want to be able to use development and production config files according to the APP_ENV variable I pass.
But even if I pass the env as a variable, it uses development for debug and production for release.
"android": "APP_ENV=development react-native run-android --variant 'devDebug'",
"android-prod": "APP_ENV=production react-native run-android --variant 'prodDebug'",
"build-android-dev": "cd android && APP_ENV=development ./gradlew assembleDevRelease",
"build-android": "cd android && APP_ENV=production ./gradlew assembleProdRelease",
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 19 (10 by maintainers)
@caiangums Thanks for the motivation to help. I think all of us read the documentation, and we all provided the
APP_ENVvariable to thereact-native startcommand. That’s essentially the source of the problem. Please re-read my above comment and check how release versions are built. Release versions on Android spawn their own packager ifbundleInYourCustomNameis set totrueinapp/build.gradle. Therefore, because it internally spawns it’s own packager, you are unable to provide any env variables, likeAPP_ENVdynamically from scripts.I ended up using react-native-config because of the following reasons:
.envfile as configurationAPP_ENValternative toreact-native run-androidcommand and not thereact-native startcommand, hence the issue I described above is non-existent.Anyway, thanks @goatandsheep for your work and your kindly responses, I just found that the other package better suits my needs.
Hey! 👋
I noticed that you are passing the
APP_ENVvariable at thereact-native run-androidcommand when it should be passed at the starting of the metro server(react-native start). It is possible to see that in this section of the README.md.I suggest changing your
scriptssection in yourpackage.jsonto something like:It is important to note that your
.envfiles should respect the pattern.env.<environment>. So, I also suggest changing your main/production.envfile to.env.mainor.env.production, so you could pass them asAPP_ENV=mainorAPP_ENV=production😄Hope that helps!
Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/131
Hi @Asranov! Turns out that I changed my approach to other libs for solving this issue.
If you want, you can try the approach mentioned by @f4z3k4s using react-native-config.
If you’re using
APP_ENV, you should avoid usingdevelopmentnorproductionas values and you should avoid having a.env.developmentnor.env.production. This is a Babel and Node thing that I have little control over unfortunately and is consistent with many other platforms that have an override option, like Gatsby. If you want to usedevelopmentandproduction, don’t useAPP_ENVas it’s built in. UseNODE_ENV=developmentorNODE_ENV=production.This has been added to the README.
I need to figure out how the other library deals with caching issues
@f4z3k4s Thanks for the clarification! I used
react-native-configbefore but the approach from this library seems easier to configure 🙏While trying to change some code at
metro.config.jsto properly pass the actual values, no success was accomplished from my side. Hope that @goatandsheep could give a better solution 😅The problem I ran into using
APP_ENV:As I understand
APP_ENVshould be provided to the packager, not therun-androidcommand. So if I runAPP_ENV=staging npx react-native startit uses.env.stagingas a result.That is all good if you have the ability to spawn a custom packager, e.g.: in debug builds. But what happens when you run a release build? If
bundleInStaginginapp/build.gradleis set totrue, it will spawn a custom packager during the build process, regardless if you have a packager running with the properAPP_ENVset. And in the custom packager’s environment,APP_ENVwill be unset. If you setbundleInStagingto false and you try to build your own bundle withAPP_ENV=staging npx react-native bundle ...it still spawns it’s own packager, and the env you specified for the bundle command doesn’t apply for the packager. So all in all, APP_ENV is unusable for release builds according to my understanding.Am I missing something or do you experience the same?
I managed to bypass this whole
APP_ENVissue by creating a short node script which copies the content of.env.stagingto.envbefore build and.envgets picked up by the bundle in release as well. I am using.env.debug,.env.stagingand.env.releaseand based on the desired release, I dynamically copy the contents of these to.env.btw, you shouldn’t need
APP_ENVforproductionanddevelopmentas those are built in. You’re better off controlling it through the variants inside your gradlew@goatandsheep unfortunately no change