0

Recently I structure my firebase functions so that the directory has a more modular approach by taking reference from another SO post.

Thus the firebase directory structure is

index.js
|
modules/
├── enterprise
│ ├── getCompanyLicenseUsers.js
│ ├── index.js
│ └── revokeLicense.js
├── premium
│ ├── applyLicense.js
│ ├── getCouponDiscount.js
│ ├── index.js
│ ├── processPaypalPayment.js
│ └── unlockPremium.js
└── utils
├── index.js
└── utilityFunctions.js

All the files inside the different modules (except for utils) are individually exported as firebase functions in the uppermost index.js. The index.js of each module makes a global export to each function.

sample function getCompanyLicenseUsers

require('../utils');
const functions = require("firebase-functions");

module.exports.getCompanyLicenseUsers = () => {
  return functions.https.onCall((data, context) => {
    // function code
  })
}

index.js of the module of which the function is a part of

global.getCompanyLicenseUsers =  require('./getCompanyLicenseUsers').getCompanyLicenseUsers
global.revokeLicense =  require('./revokeLicense').revokeLicense

the main (outermost) index.js

// Module imports 
require('./modules/enterprise')
require('./modules/notifications')
require('./modules/premium')
require('./modules/jobs')

// exporting firebase functions
exports.getCompanyLicenseUsers = getCompanyLicenseUsers();
exports.revokeLicense = revokeLicense();

exports.applyLicense = applyLicense();
exports.getCouponDiscount = getCouponDiscount();
exports.processPaypalPayment = processPaypalPayment();
exports.unlockPremium = unlockPremium();

Now I want to add another module but not all the functions of that module need to be exported. These non exported functions are imported into the function that will be exported from that module.

I am able to successfully deploy that function but whenever this exported function is executed on firebase, there is a reference error to the functions that were called inside the exported function but not explicitly exported to firebase (but were a part of the module of the exported function).

Error Message

I guess I can make these individual functions as modules of their own but I do not want to do so as the code would then seem sort of over-organized.

Thanks for even reading the whole question, if you did. Any help is much appreciated

Geek
  • 345
  • 2
  • 13

1 Answers1

0

I am having almost 30+ functions in my code base and organised in a single project (with improved cold start perf as well).

This one is most simplified form https://stackoverflow.com/a/47361132/1212903

Please refer(though it uses typescript it will give you an idea on how to handle it better) https://medium.com/firebase-developers/organize-cloud-functions-for-max-cold-start-performance-and-readability-with-typescript-and-9261ee8450f0

Codex
  • 699
  • 10
  • 24