How to Deploy Firebase Function: Step-by-Step Guide
To deploy a Firebase function, first write your function code in the
functions folder, then run firebase deploy --only functions in your terminal. This command uploads your code to Firebase and makes your function live.Syntax
The basic command to deploy Firebase functions is:
firebase deploy --only functions: Deploys only the cloud functions.firebase deploy: Deploys all Firebase services configured, including functions.
You run this command in your project root folder where firebase.json and functions folder exist.
bash
firebase deploy --only functions
Example
This example shows a simple HTTP Firebase function and how to deploy it.
javascript
const functions = require('firebase-functions'); exports.helloWorld = functions.https.onRequest((request, response) => { response.send('Hello from Firebase!'); });
Output
Function deployed and accessible via generated URL.
Common Pitfalls
Common mistakes when deploying Firebase functions include:
- Not running
npm installinside thefunctionsfolder before deploying. - Trying to deploy without initializing Firebase project with
firebase init. - Not logging in with
firebase loginor using the wrong project withfirebase use. - Deploying from the wrong folder or missing
firebase.json.
bash
Wrong: firebase deploy Right: cd functions npm install cd .. firebase deploy --only functions
Quick Reference
Remember these quick tips for smooth Firebase function deployment:
- Always run
npm installinfunctionsbefore deploying. - Use
firebase deploy --only functionsto deploy only functions. - Check your Firebase project with
firebase use. - Test functions locally with
firebase emulators:startbefore deploying.
Key Takeaways
Write your Firebase function code inside the functions folder before deploying.
Run npm install in the functions folder to install dependencies before deployment.
Use firebase deploy --only functions to deploy just your cloud functions.
Make sure you are logged in and using the correct Firebase project.
Test your functions locally with Firebase emulators before deploying to production.