menu

Expo

Build and share truly native apps that work across both iOS and Android. Open source, free and uses React Native.

Channels
Team

How do you manage your RN environment variables?

January 3, 2019 at 4:03pm

How do you manage your RN environment variables?

January 3, 2019 at 4:03pm
Hey all! Wondering how everyone manages environment variables such as api urls when publishing or building for release on the App Store/Google Play. I'm sure there are a couple of ways to do it, and I'd love to hear your thoughts
My current setup:
From what I've found, it looks like by default, it's only possible to do production and development configs (no staging or other environments) – is that right?
Right now, I've been using a environment.js file that can be used to detect expo release channels and change the variables returned based on that, but for building, I need to update the non- __DEV__ variable returned to be either staging or prod:
import { Constants } from 'expo';
import { Platform } from 'react-native';
const localhost = Platform.OS === 'ios' ? 'http://localhost:4000/' : 'http://10.0.2.2:4000/';
const ENV = {
dev: {
apiUrl: localhost,
},
staging: {
apiUrl: 'https://your-staging-api-url-here.com/'
},
prod: {
apiUrl: 'https://your-prod-api-url-here.com/'
},
}
const getEnvVars = (env = Constants.manifest.releaseChannel) => {
// What is __DEV__ ?
// This variable is set to true when react-native is running in Dev mode.
// __DEV__ is true when run locally, but false when published.
if (__DEV__) {
return ENV.dev;
} else {
// When publishing to production, change this to `ENV.prod` before running an `expo build`
return ENV.staging;
}
}
export default getEnvVars;
Alternatives
does anyone have experience using react-native-dotenv for projects built with expo? I'd love to hear your thoughts

February 27, 2019 at 9:22am
I put mine inside app.json and then extracted it from Constants.manifest
  • reply
  • like
(non-sensitive data that is)
  • reply
  • like

February 28, 2019 at 5:09pm
What about this
  • reply
  • like
TBH I don't have any idea about this ....
  • reply
  • like

September 6, 2019 at 7:50pm
, would you please, describe your workaround in details?
  • reply
  • like

September 16, 2019 at 8:11pm
we tried react-native-dotenv but it didnt work well with expo.
This is slightly convoluted but what we do with secure credentials is keep them encrypted, then use https://github.com/99designs/aws-vault to generate temporary credentials, then use terraform to generate an environment appropriate app.json. It's a bit of a convoluted nightmare but it actually works.
Short version is keep them in app.json, I think it's the only approach that works with expo
  • reply
  • like

September 23, 2019 at 8:34pm
Sounds like a nightmare indeed. I can hardly imagine myself how will expo know which app.json env key to use. Maybe somebody has an example an example of app.json with env?
  • reply
  • like