Firebase-admin-node: Error: Failed to initialize Google Cloud Firestore client with the available credentials. Must initialize the SDK with a certificate credential or application default credentials to use Cloud Firestore API

Created on 15 Jan 2020  Â·  10Comments  Â·  Source: firebase/firebase-admin-node

Using the new version _8.9.1_ of firebase-admin, firebase-functions deployments using NodeJS 10 with the Firebase CLI fail. Reverting firebase-admin to version _8.9.0_ resolves the issue.

Here is the command used to deploy functions:

$ firebase deploy --only functions

=== Deploying to 'ipregistry-dashboard-dev'...

i deploying functions
Running command: yarn --cwd functions run clean
yarn run v1.21.1
$ rm -rf dist/
Done in 0.04s.
Running command: yarn --cwd functions run lint
yarn run v1.21.1
$ tslint -p tsconfig.json
Done in 2.03s.
Running command: yarn --cwd functions run build
yarn run v1.21.1
$ yarn run lint && ./node_modules/.bin/tsc
$ tslint -p tsconfig.json
Done in 4.63s.
Running command: cp -r $RESOURCE_DIR/keyfiles $RESOURCE_DIR/dist
✔ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

Error: Failed to initialize Google Cloud Firestore client with the available credentials. Must initialize the SDK with a certificate credential or application default credentials to use Cloud Firestore API.
at FirebaseFirestoreError.FirebaseError [as constructor] (/home/X/functions/node_modules/firebase-admin/lib/utils/error.js:42:28)
at new FirebaseFirestoreError (/home/X/functions/node_modules/firebase-admin/lib/utils/error.js:220:23)
at getFirestoreOptions (/home/X/functions/node_modules/firebase-admin/lib/firestore/firestore.js:96:11)
at initFirestore (/home/X/functions/node_modules/firebase-admin/lib/firestore/firestore.js:105:19)
at new FirestoreService (/home/X/functions/node_modules/firebase-admin/lib/firestore/firestore.js:43:32)
at /home/X/functions/node_modules/firebase-admin/lib/firebase-app.js:261:20
at FirebaseApp.ensureService_ (/home/X/functions/node_modules/firebase-admin/lib/firebase-app.js:351:23)
at FirebaseApp.firestore (/home/X/functions/node_modules/firebase-admin/lib/firebase-app.js:259:28)
at Object. (/home/X/functions/dist/callables/account-activate.js:10:33)
at Module._compile (internal/modules/cjs/loader.js:776:30)

The line 10 from account-active.js is const firestoreDatabase = admin.firestore();.

Firebase admin initialization is made as follows:

import {FirebaseAdminHelper} from '../helpers/firebase-admin-helper';
const admin = FirebaseAdminHelper.initializeApp();
const auth = admin.auth();
const config = functions.config();
const firestoreDatabase = admin.firestore();

where FirebaseAdminHelper is defined as follows:

import * as admin from 'firebase-admin';

export class FirebaseAdminHelper {

    static initializeApp(): admin.app.App {
        try {
            return admin.initializeApp();
        } catch (error) {
            // Ignore "already initialized" errors.
            // This case should only happen locally while deploying, not on real environments.
            return admin.app();
        }
    }

}
bug

Most helpful comment

temp fix:

npm i [email protected]

issue only seems to affect 8.9.1

All 10 comments

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

What does npm ls firebase-admin say after the upgrade?

This happens when the local development environment has been initialized with refresh token credentials. Since you have an admin.firestore() call in the global scope, it attempts to create a Firestore client at the trigger parsing phase. There are couple of different workarounds:

  1. Move the admin.firestore() into one of the functions. You don't get any benefit from keeping it in the global scope anyway. Or,
  2. Set the GOOGLE_APPLICATION_CREDENTIALS env variable to specify a service account.

I'm not sure how the CLI is supposed to work in this case. So I'm going to move this to the CLI repo and see what they think.

Managed to come up with a fix in our code.

Same here

➜ functions git:(gastonerie2) ✗ npm ls firebase-admin
functions@ /Users/lucasgeitner/work/poesie/poesie.io_app/functions
└── [email protected]

This is how i use firestore so i can use it with the simulator :)

thanks for your help

let admin = require('firebase-admin')
let firestore
let db
let storage
const projectId = 'XX'
let auth
let clean

if (process.env.NODE_ENV !== 'test') {
  admin.initializeApp()
  firestore = admin.firestore
  db = admin.firestore()
  auth = admin.auth
  if (admin && admin.storage && admin.storage()) {
    storage = admin.storage().bucket()
  }
}

if (process.env.NODE_ENV === 'test') {
  const firebasetest = require('@firebase/testing')
  admin.initializeApp()

  if (admin && admin.storage && admin.storage()) {
    storage = admin.storage().bucket()
  }

  admin = firebasetest.initializeTestApp({projectId, databaseName: 'firestore'})
  db = admin.firestore()
  auth = admin.auth
  clean = async () => await firebasetest.clearFirestoreData({projectId})
  firestore = admin.firestore
}

module.exports = {db, admin, firestore, auth, clean, storage}

find a way to deploy and login by doing

export GOOGLE_APPLICATION_CREDENTIALS=/Users/lucasgeitner/work/**.json

i just get this error when runing firebase emulators:start :

âš   functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /Users/lucasgeitner/work/****.json. Non-emulated services will access production using these credentials. Be careful!

And as soon i leave my terminal, i have to do again the export

Still thanks for the great work around firebase ;)

temp fix:

npm i [email protected]

issue only seems to affect 8.9.1

Same problem here using :

admin.initializeApp({ credential: admin.credential.applicationDefault() });

Downgrading using yarn add [email protected] seems to get it working again.

Thanks @darinw

What worked for me was to run following command again:
$ export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"

For more information see this link

Was this page helpful?
0 / 5 - 0 ratings