英文:
configuring transform-inline-environment-variables for react-native 0.60.6
问题
module.exports = function () {
return {
"presets": ['module:metro-react-native-babel-preset'],
"plugins": [
["transform-inline-environment-variables", {
"include": [
"APP_ENV"
]
}]
]
}
};
这是我的react-native项目的babel.config.js文件,我正试图配置transform-inline-environment-variables Babel插件。但无论我运行应用程序时,环境变量始终为未定义。
我使用命令react-native start --reset-cache
来清除缓存,并使用命令APP_ENV=dev react-native run-ios
来运行项目。
但是变量仍然未定义。我在这里做错了什么?React Native版本为0.60.6。
英文:
module.exports = function () {
return {
"presets": ['module:metro-react-native-babel-preset'],
"plugins": [
["transform-inline-environment-variables", {
"include": [
"APP_ENV"
]
}]
]
}
};
This is my babel.config.js for my react-native project, and I'm trying to configure the transform-inline-environment-variables babel plugin. But whenever I run the app the environment variable is always undefined.
I run react-native start --reset-cache
to clear the cache and run the project using the command APP_ENV=dev react-native run-ios
.
But the variable is still undefined. What am I doing wrong here? The react-native version is 0.60.6
答案1
得分: 2
尝试直接记录访问(而不是解构):
console.log(process.env.APP_ENV)
而且,一旦连接了模拟器,可以然后运行 start
,所以:
yarn run-ios
- 关闭 metro bundler 终端窗口,保持模拟器开启
API_ENV=dev yarn start --reset-cache
此问题在此处有详细介绍。
更好的做法是将您的实现替换为react-native-config。
英文:
Try logging access directly (rather than destructuring):
console.log(process.env.APP_ENV)
Also, once you have the emulator connected, you can then run start
, so:
yarn run-ios
- Close the metro bundler terminal window and leave the emulator open
API_ENV=dev yarn start --reset-cache
This issue is covered in detail here.
Better still, replace your implementation with react-native-config.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论