-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv_setup.txt
More file actions
63 lines (52 loc) · 2.97 KB
/
env_setup.txt
File metadata and controls
63 lines (52 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Step 1: Install the dotenv Package
First, open your terminal and navigate to your project directory. Run the following command to install the dotenv package:
sh
Copy code
npm install dotenv
Step 2: Create a .env File
In your project's root directory, create a file named .env. This file will store your environment variables.
makefile
Copy code
# .env
FIREBASE_API_KEY=your_api_key
FIREBASE_AUTH_DOMAIN=your_auth_domain
FIREBASE_PROJECT_ID=your_project_id
FIREBASE_STORAGE_BUCKET=your_storage_bucket
FIREBASE_MESSAGING_SENDER_ID=your_messaging_sender_id
FIREBASE_APP_ID=your_app_id
Replace the placeholders with your actual Firebase configuration values.
Step 3: Update Your .gitignore File
To ensure that your .env file is not committed to your public repository, you must add it to your .gitignore file. Open or create a .gitignore file in the root directory and add the following lines:
bash
Copy code
# .gitignore
.env
node_modules/
Step 4: Initialize dotenv in Your Application
In the entry point of your Node.js application, which might be App.js, index.js, or another file, add the following line at the very top to initialize dotenv:
javascript
Copy code
require('dotenv').config();
This line must be at the top to ensure that your environment variables are loaded before any other code that requires them is executed.
Step 5: Use Environment Variables in Your Code
Now you can access your environment variables using process.env. For instance:
javascript
Copy code
// Inside your Firebase configuration file or where you initialize Firebase
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
// ... other settings
};
Step 6: Deployment and CI/CD
When deploying your application or setting up continuous integration/continuous deployment (CI/CD), you will need to configure your environment variables in your hosting provider's dashboard or CI/CD configuration.
For example, if you're using Heroku, you can set environment variables (called "config vars") in the Settings tab of your application dashboard. If using Vercel, Netlify, or similar services, they also provide a section in their settings for environment variables.
For GitHub Actions, you can set secrets in the repository's Settings under "Secrets." You can then reference these secrets in your GitHub Actions workflow file using the secrets context:
yaml
Copy code
env:
FIREBASE_API_KEY: ${{ secrets.FIREBASE_API_KEY }}
# ... other variables
Step 7: Test Your Configuration
Before deploying or sharing your repository, test your configuration locally to ensure that your application correctly reads the environment variables. You can do this by running your application locally and verifying that the Firebase functionality works as expected.
By following these steps, you ensure that your Firebase configuration and other sensitive information are kept out of your public codebase, reducing the risk of unintentional exposure.