0
0
Firebasecloud~5 mins

Cloud Functions setup in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Cloud Functions let you run small pieces of code in the cloud when something happens, like a user signing up. This setup helps you write and deploy these functions easily without managing servers.
When you want to send a welcome email automatically after a user signs up.
When you need to process images uploaded to your app without slowing down the user.
When you want to update a database record whenever a file changes.
When you want to run backend code triggered by events without managing servers.
When you want to keep your app logic secure by running it in the cloud.
Config File - package.json
package.json
{
  "name": "functions",
  "scripts": {
    "lint": "eslint .",
    "build": "tsc",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "18"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^11.10.1",
    "firebase-functions": "^4.4.0"
  },
  "devDependencies": {
    "eslint": "^8.44.0",
    "eslint-config-google": "^0.14.0",
    "typescript": "^5.1.3"
  },
  "private": true
}

This file defines the Cloud Functions project settings.

  • scripts: Commands to build, test, and deploy functions.
  • engines: Specifies Node.js version 18, the current supported version.
  • dependencies: Required Firebase libraries for functions.
  • devDependencies: Tools for code quality and TypeScript support.
Commands
Starts the setup wizard to create a Cloud Functions project in your Firebase app. It creates necessary files and folders.
Terminal
firebase init functions
Expected OutputExpected
=== Project Setup === You're about to initialize a Firebase Functions project. ? What language would you like to use to write Cloud Functions? (Use arrow keys) > JavaScript TypeScript ? Do you want to use ESLint to catch probable bugs and enforce style? Yes ? Do you want to install dependencies with npm now? Yes ✔ Wrote functions/package.json ✔ Wrote functions/index.js ✔ Wrote .eslintrc.js ✔ Wrote .gitignore ✔ Initialized Cloud Functions project. Next steps: 1. Write functions in functions/index.js 2. Deploy with firebase deploy --only functions
Uploads and activates your Cloud Functions code to Firebase so they start running in the cloud.
Terminal
firebase deploy --only functions
Expected OutputExpected
=== Deploying to 'your-project-id' === i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint > functions@ lint /workspace/functions > eslint . ✔ functions: Finished running lint. i functions: ensuring necessary APIs are enabled... ✔ functions: all necessary APIs are enabled i functions: preparing functions directory for uploading... ✔ functions: functions folder uploaded successfully ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/your-project-id/overview Function URL (helloWorld): https://us-central1-your-project-id.cloudfunctions.net/helloWorld
--only functions - Deploys only the Cloud Functions part, not other Firebase services.
Shows recent logs from your deployed Cloud Functions to help you check if they run correctly or find errors.
Terminal
firebase functions:log
Expected OutputExpected
2024-06-01T12:00:00.000Z your-project-id helloWorld: Function execution started 2024-06-01T12:00:01.000Z your-project-id helloWorld: Function execution finished
Key Concept

If you remember nothing else from this pattern, remember: initializing with 'firebase init functions' sets up your project structure and deploying with 'firebase deploy --only functions' makes your code run in the cloud.

Common Mistakes
Not running 'npm install' inside the functions folder after initialization
Your functions won't have the required libraries to run, causing errors during deployment or execution.
Run 'npm install' inside the functions directory or choose to install dependencies during 'firebase init functions' setup.
Deploying without specifying '--only functions' when you only want to update functions
This can unintentionally deploy other Firebase services, which might cause delays or unwanted changes.
Use 'firebase deploy --only functions' to deploy only your Cloud Functions.
Not checking logs with 'firebase functions:log' after deployment
You miss important runtime errors or confirmation that your functions work as expected.
Always check logs after deployment to verify function behavior and troubleshoot issues.
Summary
Run 'firebase init functions' to create the Cloud Functions project files and setup.
Write your function code in the generated files inside the functions folder.
Deploy your functions to Firebase with 'firebase deploy --only functions' to make them live.
Use 'firebase functions:log' to view logs and verify your functions run correctly.