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

how multi env works? #74

Closed
helvenk opened this issue Sep 17, 2020 · 20 comments
Closed

how multi env works? #74

helvenk opened this issue Sep 17, 2020 · 20 comments

Comments

@helvenk
Copy link

helvenk commented Sep 17, 2020

I'm sorry but I really dont know how it works. If there are two files .env.dev and .env.prod,how does dotenv choose the right one?
Does it need environment variables from command line like yarn start NODE_ENV=pro? But how to do if I build the project in xcode where I cannot use command line variables?

NOTE: .env.dev and .env.prod are both used in Release version, not in Debug. They are different from React Native global variable __DEV__.

@goatandsheep
Copy link
Owner

goatandsheep commented Sep 18, 2020

I need to add this to the readme. Sorry! Basically this is based on the Babel environments: development and production. In other words, it uses .env.production or .env.development or env.test. All that the library does is compare with the Babel environment type.

So sorry, neither dev nor prod will work, but test will!

The reason why we're not checking the React Native variable is because we don't have to. This value is inserted at build time.

@helvenk
Copy link
Author

helvenk commented Sep 21, 2020

Not quite understand, but just waiting for your docs. Thanks so much.

@goatandsheep
Copy link
Owner

@lkspc I've expanded on the docs. Let me know if that helps

@arys
Copy link

arys commented Oct 14, 2020

Hello @goatandsheep, It is still not clear to me how to use it. What exactly I need to do with Babel? I've created two files .env.development and .env.production. When I start my application with react-native start it is loading variables from .env.development, so how to load them from .env.production?

I need to add this to the readme. Sorry! Basically this is based on the Babel environments

and also link above is broken

@goatandsheep
Copy link
Owner

@arys777 sorry about the link. this is the link.

Regarding which environment you choose, this is from your CLI commands. By default, the CLI commands have NODE_ENV=development so to use production variables, it's a bit of specification.

  • react-native run-android --variant=release
  • react-native run-ios --configuration=Release
  • react-native bundle --dev=false

@arys
Copy link

arys commented Oct 15, 2020

Now I see, thanks so much for the help!

@helvenk
Copy link
Author

helvenk commented Oct 15, 2020

@goatandsheep
I'm so sorry I didn't get back to you in time.
Now I know that env comes from CLI Command Variables,but how to make multi env work in XCode outside of react-native commands?
I have no idea to inject CLI Command Variables such as NODE_ENV=production to XCode to build the app.

@goatandsheep
Copy link
Owner

goatandsheep commented Oct 15, 2020

Well the CLI is really an easy way to setup the XCode configuration. Remember that in XCode

  • Configuration=Release => NODE_ENV=production
  • Configuration=Debug (default) => NODE_ENV=development

Read more about how to set that here: https://shockoe.com/ideas/development/how-to-setup-configurations-and-schemes-in-xcode/

@helvenk
Copy link
Author

helvenk commented Oct 15, 2020

Could I have a test or staging or any other env in XCode?

@helvenk
Copy link
Author

helvenk commented Oct 15, 2020

It seems that I have found the solution in the above article, thank you very much for your help!

@goatandsheep
Copy link
Owner

I've updated the docs https://github.com/goatandsheep/react-native-dotenv/wiki/Multi-env-troubleshooting

@francisleigh
Copy link

francisleigh commented Dec 24, 2020

Can anyone point me in the right direction for getting the .env.test to work? I've created a Test Scheme and a Test build configuration but it only seems to be taking the .env.development config file.

Screenshot 2020-12-24 at 11 44 15

@francisleigh
Copy link

@lkspc did you manage to successfully have a .env.staging Scheme and Build configuration?

@goatandsheep
Copy link
Owner

@francisleigh you won't be able to set the node env environment variable in that way. You'll have to do you through your cli command. Check the troubleshooting guide https://github.com/goatandsheep/react-native-dotenv/wiki/Multi-env-troubleshooting

@francisleigh
Copy link

@goatandsheep Thanks for your reply.

Forgive me but I'm looking at your link which I have previously looked at and am still struggling to get the env.test to work...
At the moment I'm setting this in my scripts.

"ios:staging": "NODE_ENV=test && BABEL_ENV=test && react-native run-ios",

Am I completely missing the mark here? It would be great to have an example of setting up a .env.test or if anyone has managed to successfully setup a .env.staging that would be great!

@goatandsheep
Copy link
Owner

goatandsheep commented Dec 29, 2020

@francisleigh I am thinking about this. I think you're right that within XCode it is not properly acknowledging .env.test. I was thinking about if this was related to the config file, but ultimately, I don't know how if that fully solves the problem. I've been thinking about if PR #97 is viable. Let's continue the discussion there.

@pedromcunha
Copy link

So after digging into how xcode is building the js code I think I understand it enough to get it working. If you go to the xcode project and open up the "Build Phases" tab, then find the "Bundle React Native code and images" phase. In there you'll see it's executing the react-native-xcode shell script. This script runs the bundle command to bundle the js code. The only way I got it to work was by adding these if statements that change the BABEL_ENV:

if [ "${CONFIGURATION}" = "Staging" ]; then export BABEL_ENV=staging elif [ "${CONFIGURATION}" = "Release" ]; then export BABEL_ENV=production elif [ "${CONFIGURATION}" = "Debug" ]; then export BABEL_ENV=development else export BABEL_ENV=development fi

Here's a screenshot of where I put that code:
Screen Shot 2020-12-30 at 10 57 42 AM

I don't think this is the cleanest approach but at least it works. Maybe someone smarter than me can come up with a cleaner approach.

@goatandsheep
Copy link
Owner

goatandsheep commented Dec 30, 2020

I'm happy it's working for you but I'd like to leave a disclaimer to anyone reading that this is an undocumented usage of babel env to use a non standard babel env such as "staging". It could be removed over time and is probably defaulting to development type rules which could be ok. There are 3 standard babel env versions: development, production, and test. https://babeljs.io/docs/en/config-files#apienv

What's cool though is that you can use custom env types if the env name begins with "test-". This is supported so i encourage it and will probably add it to the docs.

@pedromcunha
Copy link

I see, didn't realize that those were the only supported babel environments. To use the "test-" environments would I need to change my .env file to be ".env.test-staging" ?

@goatandsheep
Copy link
Owner

@pedromcunha to be honest, I don't know but that is my best guess! Let me know how it goes.

@goatandsheep goatandsheep mentioned this issue Jan 8, 2021
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

5 participants