firebase / firebase/firebase-tools

Misleading runtime error when deploying firebase functions on emulator

Open
#6,183 0 comments 0 reactions 1 assignee Claimed by @taeold View on GitHub
emulators: functions type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

## Environment info

**firebase-tools:** 12.4.2
2nd gen firebase functions

**Platform:** macOS
**node-fetch**: 3.3.1

## Test case
import and use node fetch in 2nd gen function.

```
import * as functions from "firebase-functions"
import { defineSecret } from "firebase-functions/params";
import { HttpsError, onCall } from "firebase-functions/v2/https";
import fetch, { Headers } from 'node-fetch';
const { defineInt, defineString } = require('firebase-functions/params');

const sanityProjectId = defineString('SANITY_PROJECT_ID');
const sanityApiVersion = defineInt('SANITY_API_VERSION');
const sanityToken = defineSecret('SANITY_TOKEN');

// function for modifying feedback type in sanity
export const submitFeedbackToSanity = onCall(
{ cors: ["*"], secrets: [sanityToken] },
async request => {
if (!request.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new HttpsError(
"failed-precondition",
"The function must be called while authenticated."
)
}

const { feedback, prompt, userId, chatId } = request.data
functions.logger.log("feedback", feedback)

// make http call to sanity api
const sanityUrl = `https://${sanityProjectId.value()}.sanity.studio/v${sanityApiVersion.value()}/data/mutate/production`

const sanityMutation = {
mutations: [
{
create: {
_type: "chatFeedback",
title: (prompt as String).substring(0, 100),
prompt,
userId,
feedback,
chatId,
}
}
]
}

const sanityHeaders = {
"Content-Type": "application/json",
Authorization: `Bearer ${sanityToken.value()}`
}

// return fetch(sanityUrl, {
// method: "POST",
// headers: new Headers(sanityHeaders),
// body: JSON.stringify(sanityMutation)
// })
const test = await fetch(sanityUrl)
return;
})

```

## Steps to reproduce

Simply run the emulators from the CLI and I get an error. Commenting out the use of `fetch(sanityUrl)` gives me no errors.

## Expected behavior

That emulators start with no errors regardless if I am using `fetch(sanityUrl)`

## Actual behavior

I get a very misleading error that I already mentioned here: https://github.com/firebase/firebase-tools/issues/6148#issuecomment-1649064174

but to repeat it:
```
[debug] [2023-07-25T03:59:09.040Z] Got response code 400; body Failed to generate manifest from function source: Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.
```

I don't see how it has anything to do with the above error and it would be great if the runtime was more accurate. I'm assuming it somehow tries to restart my functions and thats when i run into that error? but I only call `initializeApp()` once ever in my functions

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.