LoginSignup
15
3

More than 3 years have passed since last update.

[Firebase Cloud Functions] Error: The default Firebase app does not exist. と出る問題

Posted at

概要

Firebase Cloud Functionsで、意外なところでちょっと詰まったのでメモ。

問題

例えば以下のようにTriggerを設定したとする。

index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { myFunc } from './myFunc';

admin.initializeApp();
exports.myTrigger = functions.https
  .onRequest(async (req, res) => {
    await myFunc();
  });
myFunc.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

const db = functions.firestore();

export async function myFunc() { /* 処理 */ }

このとき、 $ firebase serve --only functions すると

Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.

なるエラー。はて、ちゃんと admin.initializeApp(); してあるのに、なぜだろう...

解決策

initializeApp() してから import しないとだめだったようだ。

index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

import { myFunc } from './myFunc';
exports.myTrigger = functions.https
  .onRequest(async (req, res) => {
    await myFunc();
  });

教訓

先に initializeApp() しよう!

15
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
3