create-react-native-app icon indicating copy to clipboard operation
create-react-native-app copied to clipboard

Environment variables

Open hartmamt opened this issue 9 years ago • 44 comments

Is there support for adding in environment variables like create-react-app allows? I tried it the way I usually do and it didn't work, but not sure if there is something in expo that allows it.

hartmamt avatar Mar 14 '17 12:03 hartmamt

We can definitely look at adding some. Which environment variables were you trying to set?

anp avatar Mar 14 '17 17:03 anp

Would love that, too. I'm trying to switch between two possible entry points (namely the real app and https://github.com/storybooks/react-native-storybook). Currently, I work around this issue by commenting out the respective component in App.js. IMHO, it would be much cleaner if I could set an environment variable in my npm tasks and choose the component depending on that, though.

inyono avatar Mar 15 '17 08:03 inyono

We use environment variables a lot when we build so that we don't have to hardcode things like REST endpoints.

hartmamt avatar Mar 17 '17 13:03 hartmamt

Also looking for this. My use case is that I need a url that leads to my host machine where I have an api server running. I would then like to be able do something like API_HOST=http://$(ipconfig getifaddr en0):4000 npm start. Perhaps there's a better solution?

scmx avatar Mar 17 '17 13:03 scmx

I believe Create React App just exposes any variables that start with REACT_APP. I figured this would just work the same.

hartmamt avatar Mar 17 '17 14:03 hartmamt

Thanks for the input everyone! I think we have a plan to implement this, I'll post here with updates.

anp avatar Mar 17 '17 21:03 anp

Update: the path to implementing this is a little trickier than I initially thought. Specifically, it's difficult to support this in a way that will continue to work after ejecting. Keeping this open so that it's still on our radar, but it might take a little while to figure out the right way to do implement.

anp avatar Mar 21 '17 00:03 anp

Thanks for the update, @dikaiosune! Quick and silly question -- with "vanilla" React Native, I simply would use babel-plugin-transform-inline-environment-variables in my .babelrc to handle this. I see that this won't work for CRNA/Expo, as I modified my CRNA .babelrc like so:

{
  "presets": ["babel-preset-expo"],
  "plugins": ["transform-inline-environment-variables"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  }
}

I'm just curious as to how/where the Babel transformations of our CRNA/Expo code occur? I assumed that the Packager would pick up the .babelrc on my project's root, but that doesn't appear to be the case. I tried reading through the source but couldn't find the answer.

My best guess is that the Babel transforms occur right on the Expo Client itself, or the Packager has its own Babel config that is not overridable?

ericnograles avatar Mar 25 '17 19:03 ericnograles

@ericnograles I don't see why that wouldn't work in principle -- can you upload a minimal repro project somewhere so that I can try?

anp avatar Mar 27 '17 17:03 anp

Sorry for the delay, @dikaiosune, here's a sample repo: https://github.com/ericnograles/crna-sample-env-variables

You'll notice my .babelrc has the line for transform-inline-environment-variables. Basically, I have a value in /common/constants.js that should resolve to the ENVIRONMENT_CURRENT environment variable, but defaults to Dangit it didn't work! if the swap doesn't happen.

I set the environment variable like so and run yarn start

image

Also, I tried setting the environment variable as part of the start script like below:

"start": "ENVIRONMENT_CURRENT=YO react-native-scripts start",

And still no luck. I don't think I'm doing anything silly here, but you be the judge. Let me know if that's enough info to go by.

Thanks for your help!

ericnograles avatar Mar 28 '17 11:03 ericnograles

@dikaiosune so I'm an idiot. I completely forgot that the CRA process ignores all env variables that didn't start with REACT_ and looks like you guys applied the same thing, except you expect a REACT_NATIVE_ to start after I rtfm in the Environment Variables section 🤣

So, it works, huzzah!

TL;DR: For anybody following my thread, you can do this via the start script like so:

"start": "REACT_NATIVE_ENVIRONMENT_CURRENT=YO react-native-scripts start",

And it works! Be sure to set environment variables to start with REACT_NATIVE_ or it will be ignored, similar to how environment variables that don't start with REACT_ are ignored by CRA.

Note: You don't actually need to add the babel-plugin-transform-inline-environment-variables plugin above, it seems to work fine without it.

Update: Got a PR out to address the documentation gap. Hope it helps someone! https://github.com/react-community/create-react-native-app/pull/125

ericnograles avatar Mar 28 '17 11:03 ericnograles

@ericnograles you're right! I left a comment on the PR, but for the benefit of anyone who finds this thread:

As of today (3/28/17), the functionality @ericnograles described above only works in development mode within CRNA -- if you eject or publish to Expo then your code will no longer see the environment variables. Please treat this feature as experimental until we can work out ejection support and provide stable documentation.

anp avatar Mar 28 '17 19:03 anp

@ericnograles @dikaiosune Many people like me want to store environment variables in .env file instead of passing on command line or package.json. May you give us the proper way to do it.

I have tried package like zoo , react-native-config but not working.

nsisodiya avatar May 02 '17 05:05 nsisodiya

@nsisodiya - can you explain why you would like to store config for a client app in a .env file rather than a json file? As far as I can tell the only difference is that you use a different parser to load the config file. It's not like you deploy to production and then suddenly you have some server environment where your env vars are already set and available, you still need to deploy your config with your app. I'm genuinely confused.

brentvatne avatar May 02 '17 05:05 brentvatne

@brentvatne I am fine with storing variable in JSON file too. you mean, I should store in .env.json file and do

import Config from '.env.json' in js files?

I do not want to put env variable in package.json. That is my main concern !! Also, I want to keep .env or .env.json file as git-ignored file.

nsisodiya avatar May 02 '17 06:05 nsisodiya

You could just call it config.json or something, no need for .env. And then just add that to your gitignore. import Config from './config.json'

brentvatne avatar May 02 '17 06:05 brentvatne

@brentvatne @dikaiosune - My biggest pain right now is, Do I need to commit this config.json or not ? I do not want to commit this because this config.json has Server API url. now this server API url changes while development. CAse 1 - If I do not commit this config.json file, and put in gitignore then - everybody will be able to write their local API server path, but in this case, I do not know if this uncommited and gitignored file will be picked while publishing standalone app build. I am using expo. Case 2 - If I commit this config.json file, then everybody will have to overwrite this file and by they by mistake commit this file with path of local environment.

====

I am unable to get a differentiating factor where I can decide whether this app is working at my local dev environment or in published environment so that I can choose right API server.

nsisodiya avatar Jun 08 '17 09:06 nsisodiya

CAse 1 - If I do not commit this config.json file, and put in gitignore then - everybody will be able to write their local API server path, but in this case, I do not know if this uncommited and gitignored file will be picked while publishing standalone app build. I am using expo.

yes, it will be picked up when publishing, Expo has nothing to do with git or gitignores

Case 2 - If I commit this config.json file, then everybody will have to overwrite this file and by they by mistake commit this file with path of local environment

it's common practice to commit something like config.json.example and then explain in the project README under a setup section that you need to copy that and call it config.json and add your secrets to it

brentvatne avatar Jun 09 '17 19:06 brentvatne

Hey, I'd like to inject the build date / build version / git commit hash and things like that as variables of my app. What's the best way to do that? It should work for dev and prod.

slorber avatar Jun 13 '17 19:06 slorber

@nsisodiya If you insist on using a .env file, then react-native-dotenv might come in handy. @brentvatne's suggestion is probably the way to go though.

Palisand avatar Jun 14 '17 02:06 Palisand