Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Does anybody face this issue?

  • https://firebase.google.com/docs/firestore/quickstart

  • https://firebase.google.com/docs/firestore/manage-data/add-data

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
like image 817
user10945377 Avatar asked Jan 21 '19 14:01

user10945377


4 Answers

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).

like image 119
Martin Zeitler Avatar answered Nov 05 '22 14:11

Martin Zeitler


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.

like image 41
Jasper Cuvelier Avatar answered Nov 05 '22 15:11

Jasper Cuvelier


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.

like image 8
Hiranya Jayathilaka Avatar answered Nov 05 '22 16:11

Hiranya Jayathilaka


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.

like image 8
Picki Avatar answered Nov 05 '22 16:11

Picki