Organize multiple functions

As you integrate Cloud Functions into your project, your code could expand tocontain many independent functions. You may have too many functions toreasonably fit in a single file, or different teams may deploy different groupsof functions, creating a risk of one team overwriting or accidentally deletinganother team's functions. Cloud Functions offers different ways to organize yourcode to make it easier to navigate and maintain your functions.

Organize functions in codebases

You can use thecodebase property of the functions configuration object infirebase.json to manage a large collection of functions across multiplerepositories or sub-packages within a single repository monorepo setup:

# firebase.json"functions": {  "codebase": "my-codebase"  # NOTE: Codebase must be less than 63 characters and can contain only  # lowercase letters, numeric characters, underscores, and dashes.}

Thecodebase property is supported in Firebase CLI v10.7.1 and above.

Managing multiple repositories

Thecodebase property can help simplify the management of multiplerepositories. Let's examine a case where you have two different repositoriesthat deploy functions to the same Firebase project:

$tree.├──repoA│├──firebase.json│└──functions│├──index.js│└──package.json└──repoB├──firebase.json└──functions├──index.js└──package.json

Without codebase annotations, the Firebase CLI would have prompted you todelete functions defined in the other repository at the time of deploy:

$(cdrepoA &&firebasedeploy--onlyfunctions)...i  functions:preparingfunctionsdirectoryforuploading...  functions:functionsfolderuploadedsuccessfullyThefollowingfunctionsarefoundinyourprojectbutdonotexistinyourlocalsourcecode:       fn1FromRepoBfn2FromRepoB...?Wouldyouliketoproceedwithdeletion?Selectingnowillcontinuetherestofthedeployments.(y/N)

You can avoid this problem by adding a unique codebase annotation in thefunctions configuration section of thefirebase.json in each project repository:

# repoA/firebase.json"functions": {  "codebase": "repo-a"}# repoB/firebase.json"functions": {  "codebase": "repo-b"}

With codebase annotation, the Firebase CLI no longer prompts you to deletefunctions defined outside of your immediate repository:

$(cdrepoA &&firebasedeploy--onlyfunctions)...ifunctions:preparingfunctionsdirectoryforuploading...functions:functionsfolderuploadedsuccessfully#  Gleefully ignores functions from repoBifunctions:creatingNode.js16functionfnFromRepoA(us-central1)...DeployComplete!

Managing multiple source packages (monorepo)

Thecodebase property can help simplify the management of multiple sourcepackages in a single repository. Let's examine a case where you have a firebaseproject directory with function definitions spread across several sub-packages:

$tree.├──firebase.json├──teamA│├──index.js│└──package.json└──teamB├──index.js└──package.json

This setup fits the following use cases:

  • You have amonorepo setup and have different teams manage their own function definitions in an isolated package.
  • You have a function with a heavy external dependency and a long-running initialization, and want to isolate that function from other, latency-sensitive functions.

To support monrepo setup like this, define multiple functions configurationsinfirebase.json:

"functions": [  {    "source": "teamA",    "codebase": "team-a"  },  {    "source": "teamB",    "codebase": "team-b"  },]

With this configuration, the Firebase CLI deploys functions from all packagesin a single deploy command:

$firebasedeploy--onlyfunctionsideployingfunctionsifunctions:preparingcodebaseteam-afordeploymentifunctions:preparingcodebaseteam-bfordeploymentifunctions:creatingNode.js16functionteam-a:helloATeam(us-central1)...ifunctions:creatingNode.js16functionteam-b:helloBTeam(us-central1)......

You can also deploy a specific codebase:

$firebasedeploy--onlyfunctions:team-bideployingfunctionsifunctions:preparingcodebaseteam-bfordeploymentifunctions:updatingNode.js16functionteam-b:helloBTeam(us-central1)......

Write functions in multiple files

When getting started withCloud Functions you might put your first fewfunctions in a single file:

index.js

constfunctions=require('firebase-functions/v1');exports.foo=functions.https.onRequest((request,response)=>{// ...});exports.bar=functions.https.onRequest((request,response)=>{// ...});

main.py

fromfirebase_functionsimporthttps_fn@https_fn.on_request()deffoo(req:https_fn.Request)->https_fn.Response:returnhttps_fn.Response("Hello foo!")@https_fn.on_request()defbar(req:https_fn.Request)->https_fn.Response:returnhttps_fn.Response("Hello bar!")

This can become hard to manage with more than a few functions. Instead, youcan put all of your logic for each function in its own file and use yoursource file as a list of exports:

Node.js

foo.js

constfunctions=require('firebase-functions/v1');exports.foo=functions.https.onRequest((request,response)=>{// ...});

bar.js

constfunctions=require('firebase-functions/v1');exports.bar=functions.https.onRequest((request,response)=>{// ...});

index.js

constfoo=require('./foo');constbar=require('./bar');exports.foo=foo.foo;exports.bar=bar.bar;

Python

foo.py

fromfirebase_functionsimporthttps_fn@https_fn.on_request()deffoo(req:https_fn.Request)->https_fn.Response:returnhttps_fn.Response("Hello foo!")

bar.py

fromfirebase_functionsimporthttps_fn@https_fn.on_request()defbar(req:https_fn.Request)->https_fn.Response:returnhttps_fn.Response("Hello foo!")

main.py

fromfn_impl.fooimport*fromfn_impl.barimport*

This setup assumes a project directory structure like the following:

my-project├── firebase.json└── functions    ├── fn_impl    │   ├── __init__.py    │   ├── foo.py    │   └── bar.py    ├── main.py    └── requirements.txt

fn_impl: Can have any name

__init__.py: Required, but can be empty

Group functions

In many projects, functions can be separated into logical groups that shouldbe deployed and maintained together. For example, you might have a group offunctions used for reporting metrics:

metrics.js

constfunctions=require('firebase-functions/v1');exports.usageStats=functions.https.onRequest((request,response)=>{//...});exports.nightlyReport=functions.https.onRequest((request,response)=>{//...});

You can put these functions into a group when exporting them in yourindex.jsfile:

index.js

//Exportbothfunctionsfrommetrics.jsinthe"metrics"group://-metrics-usageStats//-metrics-nightlyReportexports.metrics=require('./metrics');

When deployed, functions will be prefixed with the name of their group, soin this example the functions would be namedmetrics-usageStatsandmetrics-nightlyReport.

When deploying functions you can limit the action to a single group:

firebasedeploy--onlyfunctions:metrics
Note: While#organize_functions_in_codebaseslets you benefit by skipping unmodified functions during deploy, writingfunctions in multiple files will still affect the node module for yourfunctions and you won't benefit from skipped deploys.

Next steps

To learn more aboutCloud Functions, see:

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.