英文:
How to read EAS build Secrets and Environment Variables in a expo project. Getting undefined?
问题
这是在我的 Sample-Expo-Project 中的代码。我无法使用 process.env 添加环境变量,也无法在 EAS Secrets 中添加。我已经在 EAS 中添加了 Secrets,但在构建时无法读取。在这两种情况下,我都得到了 undefined。请指出我错在哪里,我已经尝试了多天。
App.js
console.log(process.env); // 得到 { "NODE_ENV": "development" }
console.log(process.env.HELLO); // 得到 undefined
return (
<View style={styles.container}>
<Text>From Env {process.env?.HELLO}</Text>
<StatusBar style="auto" />
</View>
);
Babel 配置
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: ["transform-inline-environment-variables"],
};
};
Env 文件
HELLO=HelloEveryone
Package.json
{
"name": "sample-expo-app",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"expo": "~48.0.15",
"expo-status-bar": "~1.4.4",
"react": "18.2.0",
"react-native": "0.71.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.4"
},
"private": true
}
英文:
This is my code in a Sample-Expo-Project. I am not able to add environment variable using process.env and also in EAS Secrets. I have added secrets in EAS but cannot read while building. In both cases I am getting undefined. Please spot where I am doing wrong, I am trying from multiple days.
App.js
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
export default function App() {
console.log(process.env); // Getting {"NODE_ENV": "development"}
console.log(process.env.HELLO); // Getting undefined
return (
<View style={styles.container}>
<Text>From Env {process.env?.HELLO}</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
Babel Config
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: ["transform-inline-environment-variables"],
};
};
Env File
HELLO=HelloEveryone
Package.json
{
"name": "sample-expo-app",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"expo": "~48.0.15",
"expo-status-bar": "~1.4.4",
"react": "18.2.0",
"react-native": "0.71.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.4"
},
"private": true
}
答案1
得分: 1
我也曾为此苦恼,以下是对我最有效的解决方案:
我在一个 .env 文件中设置了我的环境变量,并在 expo.dev 网站上配置了我的项目。
https://expo.dev/accounts/[your_username]/projects/[your_project]/secrets
在我的代码中,我使用了 react-native-dotenv。
https://www.npmjs.com/package/react-native-dotenv
它很容易设置,而且有很好的文档。
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [["module:react-native-dotenv"]],
};
};
env.d.tsx
declare module "@env" {
export const HELLO: string;
}
App.tsx
import { HELLO } from "@env";
export default function App() {
return (
<View>
<Text>From Env {HELLO}</Text>
</View>
);
}
如果你不使用 TypeScript,可以跳过 env.d.tsx 部分。
现在我能够在开发和 EAS 构建中访问我的秘密,而且没有问题。我刚刚在 Android 的内部测试中上传了一个新版本,它运行良好。
希望我能帮助到某人。
致敬,
Thomas
英文:
I was also struggling with this and here is the solution that works best for me:
I set my environment variables in a .env file and on the expo.dev website under the configuration of my project.
https://expo.dev/accounts/[your_username]/projects/[your_project]/secrets
In my coding I'm using react-native-dotenv.
https://www.npmjs.com/package/react-native-dotenv
It is very easy to set up and they have a good documentation.
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [["module:react-native-dotenv"]],
};
};
env.d.tsx
declare module "@env" {
export const HELLO: string;
}
App.tsx
import { HELLO } from "@env";
export default function App() {
return (
<View>
<Text>From Env {HELLO}</Text>
</View>
);
}
If you're not using typescript you can skip the env.d.tsx part.
I'm now able to access my secrets in development and in my EAS builds without problems. I just uploaded a new version to Internal Testing on Android and it works fine.
Hope I could help someone.
Cheers,
Thomas
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论