Skip to content
bobby_dreamer

Organizing Firebase Functions

nodejs, firebase1 min read

We are going to just get on setting new firebase project using Functions.

1firebase login
2
3firebase projects:list
4
5firebase init
6-- You can select the project here
7(or)
8firebase use --add
9-- You can add project later by this technique

This should create a functions folder in the current folder

Now, there are multiple ways to organize firebase functions as per Google Firebase Docs

  1. Managing multiple repositories
  2. Managing multiple source packages (monorepo)
  3. Write functions in multiple files ( you can also group functions )

I am going with the 3rd option as it doesn't need node_modules folder under each function/repo folder. Its just centralized.

Write functions in multiple files ( you can also group functions )

Main index.js file

index.js
1const functions = require("firebase-functions");
2const admin = require('firebase-admin');
3
4admin.initializeApp(functions.config().firebase);
5
6// Local Requires
7const hello = require('./helloWorld');
8exports.helloWorld = hello.World;

Local file

helloWorld.js
1const functions = require("firebase-functions");
2
3// Create and Deploy Your First Cloud Functions
4// https://firebase.google.com/docs/functions/write-firebase-functions
5//
6exports.World = functions.https.onRequest((request, response) => {
7 functions.logger.info("Hello logs!", {structuredData: true});
8 response.send("Hello from Firebase!");
9});

Firebase commands

1-- #Testing
2firebase emulators:start
3-- Wait for 2-3 minutes. It should the link for function like below
4-- http://localhost:5001/btd-in3-20220830/us-central1/helloWorld
5
6-- (or)
7
8firebase serve --only functions
9
10-- #Deploying
11firebase deploy --only functions:helloWorld

Thanks for reading