简体   繁体   English

.env 环境变量没有被使用

[英].env Environment variables not being used

I am trying to deploy my react web app to Firebase.我正在尝试将我的反应 web 应用程序部署到 Firebase。 I have a github workflow setup in order to deploy to a preview channel when pull request is created, and then when it is merged it will deploy that to be live.我有一个 github 工作流设置,以便在创建拉取请求时部署到预览频道,然后在合并时将其部署为实时。 I have my firebase api key and other details stored in a.env file, which is in my.gitignore as to not check that into the git repository.我的 firebase api 密钥和其他详细信息存储在 a.env 文件中,该文件位于 my.gitignore 中,以免将其签入 git 存储库。 However, these variables are not being used when the app is deployed to firebase.但是,当应用程序部署到 firebase 时,不会使用这些变量。 A blank page is displayed and an error in the console saying that an incorrect API key is being used and to please use correct api key.显示一个空白页,并且控制台中出现错误提示正在使用不正确的 API 密钥,请使用正确的 api 密钥。 However, if I hardcode the exact same value into the config for firebase directly, it works without any issue.但是,如果我直接将完全相同的值硬编码到 firebase 的配置中,则它可以正常工作。 Any ideas on how to fix this?有想法该怎么解决这个吗? The.env example and how I am using it is below.下面是.env 示例以及我如何使用它。 (without the actual values in.env file of course). (当然没有 .env 文件中的实际值)。 Also, the.env file is in the root of the project as well.此外,.env 文件也位于项目的根目录中。

.env sample: .env 示例:

REACT_APP_API_KEY={{value}}
REACT_APP_AUTH_DOMAIN={{value}}
REACT_APP_DATABASE_URL={{value}}
REACT_APP_PROJECT_ID={{value}}
REACT_APP_STORAGE_BUCKET={{value}}
REACT_APP_MESSAGING_SENDER_ID={{value}}

firebase.js firebase.js

const config = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
};

I think that you should add .production at the end of the .env filename.我认为您应该在.env .production It becomes .env.production ,then build and deploy.它变成.env.production ,然后构建和部署。

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

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