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

APP_ENV doesn't work as expected #131

Closed
siemya opened this issue Mar 3, 2021 · 19 comments
Closed

APP_ENV doesn't work as expected #131

siemya opened this issue Mar 3, 2021 · 19 comments
Labels
wontfix This will not be worked on

Comments

@siemya
Copy link

siemya commented Mar 3, 2021

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",
@github-actions
Copy link

github-actions bot commented Mar 3, 2021

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

@goatandsheep
Copy link
Owner

I wonder if this is a caching issue. Try this for now https://www.npmjs.com/package/react-native-clean-project

@siemya
Copy link
Author

siemya commented Mar 18, 2021

@goatandsheep unfortunately no change

@goatandsheep
Copy link
Owner

This looks exactly like the stuff in the multi env wiki. Let me know if that helps

@siemya
Copy link
Author

siemya commented Apr 9, 2021

@goatandsheep

It doesn't really help actually.

As far as I can see I can't have the flexibility as I wrote above.

@goatandsheep
Copy link
Owner

One thing I can say is that gradlew is not react native so APP_ENV has no power there. Neither does this library. You'll have to rebuild

@goatandsheep
Copy link
Owner

btw, you shouldn't need APP_ENV for production and development as those are built in. You're better off controlling it through the variants inside your gradlew

@f4z3k4s
Copy link

f4z3k4s commented May 14, 2021

The problem I ran into using APP_ENV:

As I understand APP_ENV should be provided to the packager, not the run-android command. So if I run APP_ENV=staging npx react-native start it uses .env.staging as 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 bundleInStaging in app/build.gradle is set to true, it will spawn a custom packager during the build process, regardless if you have a packager running with the proper APP_ENV set. And in the custom packager's environment, APP_ENV will be unset. If you set bundleInStaging to false and you try to build your own bundle with APP_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_ENV issue by creating a short node script which copies the content of .env.staging to .env before build and .env gets picked up by the bundle in release as well. I am using .env.debug, .env.staging and .env.release and based on the desired release, I dynamically copy the contents of these to .env.

@samuelpetroline
Copy link

I'm having the same problem. When I try to build a release version with APP_ENV=staging the content is ignored and it always get the .env.production content

@goatandsheep
Copy link
Owner

I figured out the issue while doing some experimentation, but I'm going to write some documentation on it this weekend

@caiangums
Copy link
Contributor

Hey! 👋

I noticed that you are passing the APP_ENV variable at the react-native run-android command 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 scripts section in your package.json to something like:

{
  "scripts": {
    "start": "APP_ENV=development react-native start --reset-cache",
    "start-prod": "APP_ENV=production react-native start --reset-cache"
  }
} 

It is important to note that your .env files should respect the pattern .env.<environment>. So, I also suggest changing your main/production .env file to .env.main or .env.production, so you could pass them as APP_ENV=main or APP_ENV=production 😄

Hope that helps!

@f4z3k4s
Copy link

f4z3k4s commented Jun 21, 2021

@caiangums Thanks for the motivation to help. I think all of us read the documentation, and we all provided the APP_ENV variable to the react-native start command. 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 if bundleInYourCustomName is set to true in app/build.gradle. Therefore, because it internally spawns it's own packager, you are unable to provide any env variables, like APP_ENV dynamically from scripts.

I ended up using react-native-config because of the following reasons:

  • I've had no caching issues with that package (which drove me crazy with this)
  • I can use the environment variables from native code as well, and I only have one .env file as configuration
  • It provides APP_ENV alternative to react-native run-android command and not the react-native start command, 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.

@caiangums
Copy link
Contributor

@f4z3k4s Thanks for the clarification! I used react-native-config before but the approach from this library seems easier to configure 🙏

While trying to change some code at metro.config.js to properly pass the actual values, no success was accomplished from my side. Hope that @goatandsheep could give a better solution 😅

@goatandsheep
Copy link
Owner

I need to figure out how the other library deals with caching issues

@hoangnguyen8895
Copy link

I have same issue, hope to receive a solution from you soon @goatandsheep :D

@goatandsheep
Copy link
Owner

goatandsheep commented Jun 25, 2021

If you're using APP_ENV, you should avoid using development nor production as values and you should avoid having a .env.development nor .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 use development and production, don't use APP_ENV as it's built in. Use NODE_ENV=development or NODE_ENV=production.

This has been added to the README.

@stale
Copy link

stale bot commented Aug 24, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Aug 24, 2021
@stale stale bot closed this as completed Aug 31, 2021
@Asranov
Copy link

Asranov commented Jan 10, 2024

Hey , i'm also experiencing the same issue. Has anyone found a solution for this problem?

@caiangums
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

7 participants