simondotm / nx-firebase

Firebase plugin for Nx Monorepos
https://www.npmjs.com/package/@simondotm/nx-firebase
MIT License
175 stars 31 forks source link

Initialize firebase-admin triggers Failed to load function definition from source #133

Closed lucastnr closed 9 months ago

lucastnr commented 11 months ago

I'm currently migrating from 1.0.0 to 2.0.0-beta.1, mostly to achieve separated NX applications for functions.

I came across an error when I initialize the firebase-admin, it happens when I run either serve or emulate commands. Build works fine

Error message:

Failed to load function definition from source: FirebaseError: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error

This works

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

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info('Hello logs!', { structuredData: true });
  response.send('Hello from Firebase!');
});

This doesn't work

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

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info('Hello logs!', { structuredData: true });
  response.send('Hello from Firebase!');
});

My firebase package versions

lucastnr commented 11 months ago

I changed the esbuild to format to cjs and it worked for some reason.

simondotm commented 10 months ago

@lucastnr I've just released v2.0.0 of the plugin which now generates the same function template as the latest firebase-cli

/**
 * Import function triggers from their respective submodules:
 *
 * import {onCall} from "firebase-functions/v2/https";
 * import {onDocumentWritten} from "firebase-functions/v2/firestore";
 *
 * See a full list of supported triggers at https://firebase.google.com/docs/functions
 */

import {onRequest} from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";

// Start writing functions
// https://firebase.google.com/docs/functions/typescript

export const helloWorld = onRequest((request, response) => {
  logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});
simondotm commented 10 months ago

Similarly to the firebase CLI approach, it is left to the user to decide how they want to boot strap their function and rewrite main.ts accordingly.

Firebase CLI users are also left to decide if they want to use 1st or 2nd gen cloud functions (2nd gen is the default now).

One thing I'm curious about, is if initializeApp() needs to be called with 2nd gen functions. 🤔

simondotm commented 9 months ago

Added some docs about this here: https://github.com/simondotm/nx-firebase/blob/main/docs/nx-firebase-functions.md#firebase-function-projects

Closing this issue.