firebase-functions: "Error: Can't determine Firebase Database URL." after v1 Migration from Beta

Version info

firebase-functions: 1.0.0

firebase-tools: firebase/firebase-tools.git#ll-fixfbconfig2 (3.18.1) See firebase/firebase-tools/issues/722

firebase-admin: 5.11.0

Test case

See steps to reproduce. Related to firebase/firebase-functions/issues/216.

Steps to reproduce

  1. Create a sandbox Firebase project
  2. firebase init functions (Choose TypeScript and TSLint during setup)
  3. cd functions
  4. Add "express": "^4.16.3", in package.json
  5. rm -rf node_modules then npm install
  6. Replace index.ts with
import * as admin from 'firebase-admin';

admin.initializeApp();

exports['debug'] = require(`${__dirname}/debug.js`);
  1. Create debug.ts with
import * as express from 'express';
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';

const app = express();

app.get('/', async (request, response) => {
    try {
        await admin.database().ref().once('value') as admin.database.DataSnapshot;
    } catch (error) {
        console.error(error);

        return response.status(500).send(error);
    }

    return response.status(200).send('OK');
});

export = functions.https.onRequest((request, response) => {
    // https://github.com/firebase/firebase-functions/issues/27#issuecomment-292768599

    if (!request.path) {
        request.url = `/${request.url}`;
    }

    return app(request, response);
});
  1. npm run deploy
  2. Access created function URL in a web browser, e.g. https://us-central1-project-id.cloudfunctions.net/debug

Were you able to successfully deploy your functions?

Yes.

Expected behavior

HTTP Response

Status Code: 200 Body: OK

Actual behavior

HTTP Response

Status Code: 500 Body: {“code”:“database/invalid-argument”,“message”:“Can’t determine Firebase Database URL.”}

Firebase Functions Logs

{ Error: Can't determine Firebase Database URL.
    at FirebaseDatabaseError.Error (native)
    at FirebaseDatabaseError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at new FirebaseDatabaseError (/user_code/node_modules/firebase-admin/lib/utils/error.js:190:23)
    at DatabaseService.ensureUrl (/user_code/node_modules/firebase-admin/lib/database/database.js:75:15)
    at DatabaseService.getDatabase (/user_code/node_modules/firebase-admin/lib/database/database.js:52:26)
    at FirebaseApp.database (/user_code/node_modules/firebase-admin/lib/firebase-app.js:235:24)
    at FirebaseNamespace.fn (/user_code/node_modules/firebase-admin/lib/firebase-namespace.js:283:45)
    at Object.<anonymous> (/user_code/lib/debug.js:16:21)
    at next (native)
    at /user_code/lib/debug.js:7:71
  errorInfo: 
   { code: 'database/invalid-argument',
     message: 'Can\'t determine Firebase Database URL.' } }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 33 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same issue solved it like this

import * as admin from ‘firebase-admin’; admin.initializeApp(functions.config().firebase);

TL;DR: Though it’s a bit hacky, can you try requiring firebase-functions before calling admin.initializeApp()?

(background) We are in the process of providing a server-side contract for Cloud Functions that lets you auto-initialize the Firebase Admin SDK, but that still hasn’t completed unfortunately.

Until then, requiring firebase-functions will silently ensure the new model (the FIREBASE_CONFIG env var) on any environment deployed with the firebase CLI.

@inlined, I changed the index.ts snippet to this and it worked (even on my actual project)! 👏🏼

// https://github.com/firebase/firebase-functions/issues/220#issuecomment-379312738
// tslint:disable-next-line:no-import-side-effect
import 'firebase-functions';

import * as admin from 'firebase-admin';

admin.initializeApp();

exports['debug'] = require(`${__dirname}/debug.js`);

It kind of makes sense now since I removed import * as functions from 'firebase-functions; when I migrated to v1. I noticed I wasn’t using functions in index.ts so I removed it. I’ll retain it until your server-side contract is complete. Importing in TypeScript is such a nuisance; I still can’t get my head around it. For now, I’ll monitor the health of my functions in the coming days. Thanks for the new health dashboard in the Firebase console, and of course thank you for the insight into this issue. 🎉

I just got the following working in my index.ts, although it looks like firebase-functions is favouring some internal firebase config call and if else’ing to this:

import { AppOptions, initializeApp } from 'firebase-admin';

initializeApp({
    databaseURL: `https://${process.env.GCLOUD_PROJECT}.firebaseio.com`,
    storageBucket: `${process.env.GCLOUD_PROJECT}.appspot.com`,
    projectId: process.env.GCLOUD_PROJECT,
});

Would you recommend sticking with this or going with the require(‘firebase-functions’)? 🤔 Thanks for the help

Just want to add that adding firebase functions before initializeApp() worked for me.

The problem:

'use strict';

global['firebase_admin'] = require('firebase-admin');
firebase_admin.initializeApp();

The solution

'use strict';
global['firebase_functions'] = require('firebase-functions');
global['firebase_admin'] = require('firebase-admin');
firebase_admin.initializeApp();

Yeah I’ve been meaning to make it dynamically pull in the latest versions from npm… but it keeps falling behind on my to do list. Great chance to make a PR if anyone has some time on their hand!

I’m in the same situation except I’m using javascript. My first three lines are:

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

And I’m also getting the “Can’t determine Firebase Database URL” error when a firebase function is called. Specifically, when admin.database() is called. version 3.18.4

We are in the process of providing a server-side contract for Cloud Functions that lets you auto-initialize the Firebase Admin SDK, but that still hasn’t completed unfortunately.

@inlined Why isn’t this documented anywhere in the docs (at least doesn’t seem like it)?