We are in need of adjusting specific configuration/variables within our React Native app (created with Expo) based on the environment (local/dev/staging/production). After exploring various libraries designed for this purpose, we have encountered issues that do not align with our requirements:
- dotenv (issues arise as it attempts to access 'fs' at runtime, which is no longer available due to being a non-pure JS package that cannot be bundled by Expo)
- react-native-config (cannot be integrated with Expo since it requires native code as part of the plugin)
- react-native-dotenv (partially functions but internally caches config and does not recognize any .env changes until the importing file is modified)
Instead of relying on third-party plugins, I am contemplating utilizing babel's env option and creating separate JSON objects for each environment within babel.config.js
. However, there seems to be limited documentation or examples regarding this approach. Should I simply include an env
field on the same level as presets
and plugins
, containing production
, development
, etc. fields as shown in the example below:
module.exports = (api) => {
api.cache(true);
return {
presets: [...],
env: {
development: {
CONFIG_VAR: 'foo'
},
production: {
CONFIG_VAR: 'bar'
}
}
}
}
Would this method be effective? And how can I access the CONFIG_VAR
later in the code?