-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Environment variables #57
Comments
We can definitely look at adding some. Which environment variables were you trying to set? |
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 |
We use environment variables a lot when we build so that we don't have to hardcode things like REST endpoints. |
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 |
I believe Create React App just exposes any variables that start with REACT_APP. I figured this would just work the same. |
Thanks for the input everyone! I think we have a plan to implement this, I'll post here with updates. |
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. |
Thanks for the update, @dikaiosune! Quick and silly question -- with "vanilla" React Native, I simply would use
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 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 I don't see why that wouldn't work in principle -- can you upload a minimal repro project somewhere so that I can try? |
Sorry for the delay, @dikaiosune, here's a sample repo: https://github.com/ericnograles/crna-sample-env-variables You'll notice my I set the environment variable like so and run Also, I tried setting the environment variable as part of the
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! |
@dikaiosune so I'm an idiot. I completely forgot that the CRA process ignores all env variables that didn't start with So, it works, huzzah! TL;DR: For anybody following my thread, you can do this via the
And it works! Be sure to set environment variables to start with Note: You don't actually need to add the Update: Got a PR out to address the documentation gap. Hope it helps someone! #125 |
@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. |
@ericnograles @dikaiosune I have tried package like zoo , react-native-config but not working. |
@nsisodiya - can you explain why you would like to store config for a client app in a |
@brentvatne I am fine with storing variable in JSON file too.
I do not want to put env variable in package.json. That is my main concern !! |
You could just call it |
@brentvatne @dikaiosune - My biggest pain right now is, Do I need to commit this config.json or not ? ==== 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. |
yes, it will be picked up when publishing, Expo has nothing to do with git or gitignores
it's common practice to commit something like |
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. |
@nsisodiya If you insist on using a |
@slorber - so this isn't really related to Create React Native App, it's more of an Expo thing, but Expo gives you 1) build date 2) a revisionId for every build, which is unique and we generate for you 3) the version that you put in app.json/exp.json. You can access these through As for a commit hash -- you'd need to add that somewhere in your app.json/exp.json (under the extra key) or drop it into a js file before publishing. |
Thanks this will solve my usecase, but it would be nice to have a flexible option where we can compute with JS the vars to inject. It can become a bit messy to modify/revert the exp.json file on every publish |
@slorber - it sounds like what you want is some kind of prePublish hook where you can add metadata to the |
yes it probably works but exp.json is versionned and I don't necessarily want to persist this change |
For @slorber and anyone else who may find this useful, this is how I have resolved to handle not tracking my environment variables but still generating an app.json file from a tracked configuration source. Feedback welcome! https://gist.github.com/joelbowen/1d2f2dfa471efad2154e6318c195b77e TL;DR I create a |
To anyone who has scrolled down here still having issues with environment variables for different scripts. I have made a solution that worked for myself for React Native Versions
Package.jsonFollowing @ericnograles solution
Code
I had to add --reset-cache because when running a different script it would still have the previous scripts variable if the file variable.js did not change. Using --reset-cache removed everything so it takes a minute to reload everything again but at least it works. Use babel transform-inline-environment-variables plugin. EDITIf you don't want to add --reset-cache in your package.json. You can also run this by doing
|
@SrdjanCosicPrica are you using CRNA by any chance? |
@fatahn Yes and it is ejected. Any reason why you ask? |
I'm just leaving this here for people to not ruin their night when trying to get the basic First, you have to choose a library that can parse the Combining these methods, .env parser, node cli require module and npx pass node option, we can setup our scripts similar to this.
It might not be the best solution, but this works in the current environment when starting CRNA from cli (not Expo XDE). I also have no clue what happens when creating an Android or iOS standalone app. But if you are looking for a working |
Just use https://github.com/luggit/react-native-config |
@NachtRitter As much as I like how it's supposed to work, it still doesn't work for non-ejected expo apps. I really don't want to eject as it will become harder to maintain it myself. If only there was a way to use luggit without ejecting! 😢 |
@byCedric Can you show how you access your .env variables in your code after setting things up with NPX and dotenv? |
I was able to get it working, I had to fully restart Expo so that it would reload my .env file and now I can access environment variables with The hot reloading doesn't reload the .env file though so after the first run my environment variable go away. Haven't found a solution for that yet, but thanks @byCedric! |
Hey guys, So, I ended up using react-native-dotenv for my CI needs and local dev, and it works like a charm. So, assuming you have setup a CI provider of your choice (Circle, Travis, Codeship, GitLab, BB, Heroku, whatever), I took the following steps
# Login to expo
yarn run exp login -u $EXP_USERNAME -p $EXP_PASSWORD
# Deploy to the right release channel
yarn run exp publish --release-channel $EXP_RELEASE_CHANNEL ...of course, assuming I've defined the env vars defined above. Anyway, it works great, and I've got auto publishes to the various release channels I've got defined. For my local developers, I just have a standard |
@ericnograles You might want to add a @officert I'm glad you got that one working! Now we had some trouble in with the environment when publishing it to Expo. After intensive testing en prototyping we decided to go for
Hope this helps anyone! Cheers |
I've tried adding |
@kiorq any update, i have same issue nothing works |
Nope, not any luck what so ever. I have to manually change a const in a config.js file that switches between config.dev.js or config.prod.js depending on the value of the const. :/ |
@kiorq @Sunil6591 with |
In a web react app it's possible to set env variables through .env, thanks to create-react-app, wich contains react-scripts. With create-react-app: With ejected create-react-native-app: It's possible to setup something like this in a ejected create-react-native-app? |
any updates? |
https://github.com/luggit/react-native-config I have done something like which is mentioned in this link. But when I change the .env file on the fly and just reload the application it won’t work because it bundles with the earlier env variables. Can you suggest a way to change that? |
For folks still reading this, if you just need a simple "is dev or prod" check, looks like you can use the |
Its works with restarting ios simulator at my environment. Run it with yarn: |
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.
The text was updated successfully, but these errors were encountered: