简体   繁体   English

如何将Node环境变量传递给React Native JS代码

[英]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. 我试图从React Native的JS代码访问节点环境变量,但不管怎么说,我传递它不会被使用。

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. 我希望process.env.NODE_ENV等于test但是根据bundle请求的__DEV__参数将其覆盖到productiondevelopment

What is the suggested way of passing environment variables to JS code in React Native? 在React Native中将环境变量传递给JS代码的建议方法是什么?

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. 我相信你可能已经离开了这个(5个月后),但我会回答它,因为其他人在这里找到了自己。

At best you'll set an env var in that react-native process which won't even set it in the packager . 最好你将在反应原生进程中设置一个env var,它甚至不会在包装器中设置它 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). 你不能通过这种方式设置变量的原因是该节点没有运行你的应用程序,它只是打包它并且它是在chrome webworker(在远程开发模式下)或WebKit中运行(在开启的情况下)设备)。

To do what you want, see here 要做你想做的,请看这里

To read more about the JS environment, read here 要阅读有关JS环境的更多信息,请阅读此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM