I am trying to access node environment variable from JS code of React Native, but somehow whatever I pass it is not used.
If I try something like:
NODE_ENV=test react-native run-android
I expect process.env.NODE_ENV
to be equal to test
however it is overwritten to production
or development
according to the __DEV__
param of the bundle request.
What is the suggested way of passing environment variables to JS code in React Native?
Thanks
I'm sure you have probably have moved on from this (5 months later), but I'll answer it incase anyone else has found themself here.
At best you'll set an env var in that react-native process which won't even set it in the packager. However, that doesn't seem like what you want. You want, if I understand correctly, to have access to that variable in your app code. The reason you can't do that by setting a variable that way is that node isn't running your app, it's just packaging it and it's being run in a chrome webworker (in remote dev mode) or WebKit (in the case of on device).
To do what you want, see here
To read more about the JS environment, read here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With