如何在 expo 项目中读取 EAS 构建的密钥和环境变量?返回 undefined?

huangapple go评论61阅读模式
英文:

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 &quot;expo-status-bar&quot;;
import { StyleSheet, Text, View } from &quot;react-native&quot;;

export default function App() {
  console.log(process.env); // Getting {&quot;NODE_ENV&quot;: &quot;development&quot;}
  console.log(process.env.HELLO); // Getting undefined

  return (
    &lt;View style={styles.container}&gt;
      &lt;Text&gt;From Env {process.env?.HELLO}&lt;/Text&gt;
      &lt;StatusBar style=&quot;auto&quot; /&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: &quot;#fff&quot;,
    alignItems: &quot;center&quot;,
    justifyContent: &quot;center&quot;,
  },
});

Babel Config

module.exports = function (api) {
  api.cache(true);
  return {
    presets: [&quot;babel-preset-expo&quot;],
    plugins: [&quot;transform-inline-environment-variables&quot;],
  };
};

Env File

HELLO=HelloEveryone

Package.json

{
  &quot;name&quot;: &quot;sample-expo-app&quot;,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;main&quot;: &quot;node_modules/expo/AppEntry.js&quot;,
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;expo start&quot;,
    &quot;android&quot;: &quot;expo start --android&quot;,
    &quot;ios&quot;: &quot;expo start --ios&quot;,
    &quot;web&quot;: &quot;expo start --web&quot;
  },
  &quot;dependencies&quot;: {
    &quot;expo&quot;: &quot;~48.0.15&quot;,
    &quot;expo-status-bar&quot;: &quot;~1.4.4&quot;,
    &quot;react&quot;: &quot;18.2.0&quot;,
    &quot;react-native&quot;: &quot;0.71.7&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@babel/core&quot;: &quot;^7.20.0&quot;,
    &quot;babel-plugin-transform-inline-environment-variables&quot;: &quot;^0.4.4&quot;
  },
  &quot;private&quot;: 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: [&quot;babel-preset-expo&quot;],
    plugins: [[&quot;module:react-native-dotenv&quot;]],
  };
};

env.d.tsx

declare module &quot;@env&quot; {
  export const HELLO: string;
}

App.tsx

import { HELLO } from &quot;@env&quot;;

export default function App() {

  return (
    &lt;View&gt;
      &lt;Text&gt;From Env {HELLO}&lt;/Text&gt;
    &lt;/View&gt;
  );
}

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

huangapple
  • 本文由 发表于 2023年5月21日 18:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定