expo-env
v1.1.1
Published
Multiple environment solution for Expo based React Native apps
Downloads
2,011
Readme
expo-env
.env solution for React Native apps Built with EXPO
Expo does not give us much in terms of managing multiple environment based configuration. Atleast other than
production
anddevelopment
. To overcome that, this is simply a workaround until a proper support is released by good folks at Expo.io.
How it works
Expo
passes the options available inapp.json
file at the root of your React Native application, along with some other details, called asmanifest
and can be accessed byExpo.Constants.manifest
- We simply use this to pass our environment based variables and configuration using the
extra
field inside thatmanifest
. - We create a new
app.json
file reading the[environment].config.js
file and passing it within thatextra
parameter.
What it involves
- Firstly we would need to make our
app.json
to not be pushed to version control (i.egit
), because whenever we will change our environment, we would be updating this file, so it will not make much sense to check this into you repository. - You will also create a configuration file for each of the environments you have. For ex. if you have
staging
,production
anddev
environments, you would create 3 files namedstaging.config.js
,production.config.js
anddev.config.js
- Every time there is a need to change the
env
we would simply run this module so that it injects properoptions
withinapp.json
, and then restart ourpackager
using eitherexp
CLI or through theXDE GUI
tool.
Making it more seamless
- TO make it more seamless, we can simple add these as
npm scripts
in ourExpo
app'spackage.json
, such as:
package.json
...
"scripts": {
...pre existing scripts,
"env": "expo-env --env=development",
"env:prod": "expo-env --env=prod"
}
...
sample config file - i.e staging.config.js
module.exports = {
"endpoint": "https://some-endpoint.someapi.com",
"AWSKey": "Avsgvdye523"
}
You would be getting the above config/ENV vars inside your application if you use Expo.Constants.manifest.extra
to get those values.
Options / Arguments
--env=[env]
: Specifies the environment config to be picked up. (default:development
)--configPath=[path]
: The path to all theenv
files. for ex.--configPath=./config
will look into theconfig
folder. (default:./
)--template=[templatename]
: The Name postfix for the env files. i.e [env].[templatename]. If values passed is.env.js
it will look for[env].env.js
in theconfigPath
. (default:config.js
)