Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail
Asked Answered
D

9

43

When I follow the document of firebase, but I can not connect to firebase.

Does anybody face this issue?

My code :

  • index.js

    var admin = require("firebase-admin");
    var serviceAccount = require("./key/serviceAccountKey.json");
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://sampleLink.firebaseio.com",
    });
    const functions = require("firebase-functions");
    
    var db = admin.firestore();
    var data = {
        name: "Los Angeles",
        state: "CA",
        country: "USA",
    };
    
    // Add a new document in collection "cities" with ID 'LA'
    var setDoc = db.collection("cities").doc("LA").set(data);
    setDoc;
    
  • package.json

    {
    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts": {
        "serve": "firebase serve --only functions",
        "shell": "firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log"
    },
    "dependencies": {
        "firebase": "^5.7.3",
        "firebase-admin": "~6.0.0",
        "firebase-functions": "^2.1.0"
    },
    "private": true
    }
    
  • console

    tktktk:functions dev$ node index.js
    Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail
    
    The behavior for Date objects stored in Firestore is going to change
    AND YOUR APP MAY BREAK.
    To hide this warning and ensure your app does not break, you need to add the
    following code to your app before calling any other Cloud Firestore methods:
    
    const firestore = new Firestore();
    const settings = {/* your settings... */ timestampsInSnapshots: true};
    firestore.settings(settings);
    
    With this change, timestamps stored in Cloud Firestore will be read back as
    Firebase Timestamp objects instead of as system Date objects. So you will also
    need to update code expecting a Date to instead expect a Timestamp. For example:
    
    // Old:
    const date = snapshot.get('created_at');
    // New:
    const timestamp = snapshot.get('created_at');
    const date = timestamp.toDate();
    
    Please audit all existing usages of Date when you enable the new behavior. In a
    future release, the behavior will change to the new behavior, so if you do not
    follow these steps, YOUR APP MAY BREAK.
    

My env is below:

$ firebase --version
6.3.0
$ node -v
v8.12.0
$ npm -v
6.4.1
Diddle answered 21/1, 2019 at 14:50 Comment(3)
To get rid of that long warning, add a line firebase.firestore().settings({timestampsInSnapshots: true});. See #51480179. I'm not sure about the warning about FIREBASE_CONFIG though. What do you mean with " I can not connect to firebase"?Monamonachal
Hi Frank, Thank you for the advice. It works to get rid of the long warning. But I still can not save the data because of missing FIREBASE_CONFIG.Diddle
I am facing this issue more than one month, still can not do any hello world thing.Diddle
D
11

That FIREBASE_CONFIG warning hints that the path to the JSON is missing (or wrongful).

Either setup FIREBASE_CONFIG, as it demands - or setup GOOGLE_APPLICATION_CREDENTIALS as environment variable and then run gcloud auth application-default login; then you could use admin.credential.applicationDefault() instead of admin.credential.cert(serviceAccount).

Dentist answered 22/1, 2019 at 19:24 Comment(0)
B
8

The proper way to run a cloud function locally is via the Functions shell (see https://firebase.google.com/docs/functions/local-emulator). If you want to run it directly like $ node index.js, then you have to set the required environment variables yourself, or otherwise firebase-functions is going to complain.

However, note that the above message is just a warning. Your code is running despite that. If you're not seeing any data written to Firestore, that's probably because you're not handling the promise returned by the Firestore set() method.

Bardo answered 22/1, 2019 at 19:17 Comment(1)
sir could you please help with this one #66965066 it seems similarContinuous
M
8

For me, the problem was that line:

const functions = require('firebase-functions');

I just deleted it from index.js. I don't use Firebase Cloud Function anymore but I forgot to remove that line. This is where my problem was coming from.

So maybe you too imported this package in a file that don't need it.

Meliamelic answered 23/8, 2020 at 22:38 Comment(1)
This was a cause of problem for me as well.Rumelia
F
7

I had the exact same issue. Turns out it has to do with Node version 10 I removed Node 10 and went back to Node 8 and everything worked like a charm...

 const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 admin.initializeApp();

Just hit

 firebase deploy --only functions

in Node v8.

Foreclosure answered 16/5, 2019 at 12:2 Comment(1)
The asker says they're using Node v8.12.0Tweed
H
2

About you problem, just now Thanks to your code i was able to fix my problem.

What i do:

const admin = require('firebase-admin');

var serviceAccount = require("./serviceAccount.json");

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

const db = admin.firestore();

Im using a Cloud Firestore db, not a real time db, so i dont send the second argument in the initializeApp, i just put the config of the db in the environment file.

export const environment = {
  production: false,
  firebaseConfig : {
    apiKey: 'XXXXXXXXXXX',
    authDomain: 'XXXXXXXXXX',
    databaseURL: 'XXXXXXXXXX',
    projectId: 'XXXXXXXXX',
    storageBucket: 'XXXXXXXXXXX',
    messagingSenderId: 'XXXXXXXXXXX',
    appId: 'XXXXXXXXXXXX',
    measurementId: 'XXXXXXX'
  }
};
Hittel answered 3/6, 2020 at 21:0 Comment(0)
P
1

This worked for me :

admin.initializeApp({
  credential: admin.credential.cert({
      "type": "service_account",
      "project_id": "took from Firebase Generated Private Key",
      "private_key_id": "took from Firebase Generated Private Key",
      "private_key": "took from Firebase Generated Private Key",
      "client_email": "took from Firebase Generated Private Key",
      "client_id": "took from Firebase Generated Private Key",
      "auth_uri": "took from Firebase Generated Private Key",
      "token_uri": "took from Firebase Generated Private Key",
      "auth_provider_x509_cert_url": "took from Firebase Generated Private Key",
      "client_x509_cert_url": "took from Firebase Generated Private Key"
  }),
  databaseURL: "https://test-dia.firebaseio.com"
});
Perlman answered 24/11, 2021 at 23:50 Comment(0)
S
0

The correct way to remove the warning as of 2021 is to add firebase-functions-test to your dev-dependencies, and call it in your jest config like so:

// jest.preset.js
const test = require('firebase-functions-test')();
// ...

Read more about testing in "offline mode" here: https://firebase.google.com/docs/functions/unit-testing#offline-mode

Selfness answered 5/12, 2021 at 19:25 Comment(0)
R
0

If you are trying to run a Cloud Function locally and want to use cloud authentication and database, eg.

Steps: 1 - Go to your Firebase console and copy your credentials data as JSON, for example:

{
      "apiKey": "<YOUR-API-KEY>",
      "authDomain": "<YOUR-authDomain>",
      "databaseURL": "<YOUR-databaseURL>",
      "projectId": "...",
      "storageBucket": "...",
      "messagingSenderId": "...",
      "appId": "..."
}

2 - On your project folder, save this as JSON file. 'my_credentials.json' for example.

3 - Run Function Locally: (note the cat)

$ FIREBASE_CONFIG=$(cat my_credentials.json) npm run local

Note (!): Add the 'local' script to your package.json file:

  "scripts": {
   ...
   "local": "npx functions-framework --target=<YOUR-APP-NAME> --signature-type=<YOUR-FUNCTION-CALL-METHOD-EXAMPLE-http>"
 },
Reliquary answered 2/12, 2022 at 15:23 Comment(1)
Extra: put all together on script/local package.json: "local": "FIREBASE_CONFIG=$(cat credentials.json) npx functions-framework --target=app --signature-type=http"Reliquary
A
-1

Try using this version dependencies (in package.json) with node v8.

"dependencies": {
    "firebase-admin": "~7.1.1",
    "firebase-functions": "^2.2.1"
}

It worked for me.

Apterygial answered 3/10, 2019 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.