'environment variable not working in react firebase config file
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
when it should be logging
NODE_ENV: "production"
PUBLIC_URL: ""
WDS_SOCKET_HOST: undefined
WDS_SOCKET_PATH: undefined
WDS_SOCKET_PORT: undefined
GOOGLE_API_KEY: "key"
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.
Solution 1:[1]
https://create-react-app.dev/docs/adding-custom-environment-variables/
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_
.
Note: You must create custom environment variables beginning with
REACT_APP_
. Any other variables exceptNODE_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.
Solution 2:[2]
- create .env file in your root where package.json located.
- make variable name with REACT_APP_customName/cofigString
- replace the values with the variable (2)
- save both files
- check the project
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Drew Reese |
Solution 2 | fiverrtahmid |