Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Node environment variables to React Native JS code

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

like image 953
cubbuk Avatar asked Nov 09 '22 03:11

cubbuk


1 Answers

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

like image 155
klvs Avatar answered Nov 14 '22 22:11

klvs