You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS EC2上初始化多应用的Firestore?解决Node.js部署报错

Fixing "FIREBASE_CONFIG environment variable is missing" Warning on AWS EC2

Hey there, let’s sort out that Firebase config warning you’re seeing when running your Node.js push notification backend on AWS EC2. This error pops up because the Firebase Admin SDK needs valid authentication details to connect to your Firestore database, and right now it can’t find those details automatically.

Why This Warning Happens

When initializing firebase-admin, it looks for either:

  • The FIREBASE_CONFIG environment variable containing your service account’s JSON config (as a condensed string), or
  • The GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to your downloaded service account JSON file.

If neither is set, it throws that warning and fails to initialize—this will break your push notification functionality if left unaddressed.

Step-by-Step Solutions

1. First, Grab Your Firebase Service Account Key

Before diving into fixes, get your service account JSON file from the Firebase Console:

  • Navigate to your Firebase project → Project Settings → Service Accounts
  • Click Generate New Private Key and download the JSON file (keep this secure—it’s sensitive data!)

2. Option 1: Temporary Fix (For Quick Testing)

Upload the JSON file to your EC2 instance (use scp or a file transfer tool) to a directory like /home/ubuntu/firebase-key.json. Then run these commands in your terminal:

# Point the SDK to your key file
export GOOGLE_APPLICATION_CREDENTIALS="/home/ubuntu/firebase-key.json"
# Start your Node.js server
node index.js

This works for the current terminal session, but you’ll need to re-run the export command if you open a new session.

3. Option 2: Permanent Fix (For Production)

To make the environment variable persist across terminal sessions:

  1. Open your shell profile file (e.g., ~/.bashrc or ~/.profile) with a text editor:
    nano ~/.bashrc
    
  2. Add this line at the bottom, replacing the path with your actual key file location:
    export GOOGLE_APPLICATION_CREDENTIALS="/home/ubuntu/firebase-key.json"
    
  3. Save and exit, then apply the changes immediately:
    source ~/.bashrc
    

Now every time you start your server, the environment variable will load automatically.

4. Option 3: Load the Key Directly in Code (Avoid If Possible)

If you prefer not to use environment variables, you can load the key directly in your index.js file. Just never commit this code to version control—it’s sensitive:

const admin = require('firebase-admin');
// Replace the path with your key file's location on EC2
const serviceAccount = require('/home/ubuntu/firebase-key.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

Critical Security Note

Always restrict access to your service account key file to prevent unauthorized use:

chmod 600 /home/ubuntu/firebase-key.json

This sets permissions so only your user can read the file.

After trying any of these options, restart your Node.js server—the warning should disappear, and your Firebase Admin SDK will initialize correctly to connect to Firestore.

内容的提问来源于stack exchange,提问作者PvDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:20:32