环境变量在React Firebase配置文件中不起作用 [英] environment variable not working in react firebase config file

本文介绍了环境变量在React Firebase配置文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase制作React应用,并且我想在Firebase配置中使用环境变量.这是我的react apps src文件夹中的firebase配置

I am making a react app with firebase and I want to use environment variables for the firebase config. this is the firebase config in my react apps src folder

import firebase from "firebase"
import "firebase/auth"

console.log(process.env)

const config = {
    //private stuff
};
// Initialize Firebase
firebase.initializeApp(config);
firebase.analytics();

export default firebase

问题在于该控制台日志,我正在使用它进行调试,它正在记录

the issue is with that console log, I am using that for debugging, it is logging

NODE_ENV: "production"
PUBLIC_URL: ""
WDS_SOCKET_HOST: undefined
WDS_SOCKET_PATH: undefined
WDS_SOCKET_PORT: undefined

何时应该记录

NODE_ENV: "production"
PUBLIC_URL: ""
WDS_SOCKET_HOST: undefined
WDS_SOCKET_PATH: undefined
WDS_SOCKET_PORT: undefined
GOOGLE_API_KEY: "key"

为什么我的自定义环境变量没有显示?我正在使用AWS Amplify进行部署,因此可以提供环境变量.

why aren't my custom environment variables showing up? I am using AWS amplify for deployment so that is what would be providing the environment variables.

推荐答案

您的项目可以使用在您的环境中声明的变量,就像它们是在您的JS文件中本地声明的.默认情况下,您将拥有为您定义的 NODE_ENV ,以及任何其他环境变量使用 REACT_APP _ .

Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_.

注意:您必须创建以以下内容开头的定制环境变量 REACT_APP _ .除 NODE_ENV 以外的任何其他变量将被忽略为避免意外地在机器上公开私钥,这可能会具有相同的名称.更改任何环境变量将需要您可以重新启动开发服务器(如果正在运行).

Note: You must create custom environment variables beginning with REACT_APP_. Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

这篇关于环境变量在React Firebase配置文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆