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

https://github.com/zetachang/react-native-dotenv


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