nx-react-native icon indicating copy to clipboard operation
nx-react-native copied to clipboard

NX Environment Variables

Open ceremonious opened this issue 4 years ago • 4 comments

Are we able to access NX environment variables from the React Native JS bundle? I have an environment variable defined in .local.env and I'm able to access it in my React & NodeJS apps in the same workspace. However, the same variable is undefined when I try to access it from the JS bundle during local development. The variable is defined if I print it out in metro.config.js, so it seems to be available during build time.

ceremonious avatar Jan 11 '21 22:01 ceremonious

@ceremonious You mean variables prefixed with NX_? We can look into this support out of the box to keep things consistent with the rest of Nx plugins.

As a workaround, this babel plugin should work, but you have to maintain the list of variables yourself. https://www.npmjs.com/package/react-native-dotenv

jaysoo avatar Jan 15 '21 15:01 jaysoo

https://www.npmjs.com/package/react-native-dotenv doesn't work as expected. We encountered following problems:

  1. Multiple environments don't work. We created .env.staging, tried to access it via APP_ENV=staging
  2. We have to make some change to .js file for new values to load whenever we change .env

Is there any other way, to pass different/new environment variables to react-native app?

rjvim avatar Jan 30 '21 07:01 rjvim

No at this moment, I'll get someone to investigate this.

jaysoo avatar Feb 10 '21 16:02 jaysoo

We ended up using - react-native-config

And we send env file using ENVFILE=.env.staging nx run-android investor-mobile

Hope this helps

rjvim avatar Feb 11 '21 04:02 rjvim