0
0
Firebasecloud~5 mins

Scheduled functions in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your app to do things automatically at certain times, like sending reminders or cleaning up old data. Scheduled functions let you run code on a timer without needing a user to start it.
When you want to send daily summary emails to users automatically.
When you need to clean up expired data every night.
When you want to update statistics every hour without manual work.
When you want to run a backup process every week.
When you want to check for inactive users and notify them monthly.
Config File - firebase.json
firebase.json
{
  "functions": {
    "source": "functions"
  },
  "emulators": {
    "functions": {
      "port": 5001
    }
  }
}

This file tells Firebase where your cloud functions code lives and sets up the local emulator port for testing functions.

Commands
This command sets up the functions folder and configuration files needed to write and deploy cloud functions.
Terminal
firebase init functions
Expected OutputExpected
=== Functions Setup === You're about to initialize a Firebase Functions project. ? What language would you like to use to write Functions? JavaScript ? 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 firebase.json Initialization complete!
Change directory to the functions folder where your cloud functions code is located.
Terminal
cd functions
Expected OutputExpected
No output (command runs silently)
Install the latest Firebase Functions and Admin SDK packages needed to write and deploy functions.
Terminal
npm install firebase-functions@latest firebase-admin@latest
Expected OutputExpected
+ firebase-functions@4.3.0 + firebase-admin@11.10.1 added 50 packages from 30 contributors and audited 50 packages in 3.2s found 0 vulnerabilities
Deploy your cloud functions, including scheduled functions, to Firebase so they run 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 predeploy script. ✔ functions: functions folder uploaded successfully. ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/your-project-id/overview
--only functions - Deploy only the cloud functions without other Firebase resources
Key Concept

Scheduled functions run your code automatically at set times without needing manual triggers.

Common Mistakes
Not importing the scheduler module from firebase-functions.
Without importing, the scheduled function cannot be defined and will cause errors.
Always import 'pubsub' from 'firebase-functions' to define scheduled functions.
Forgetting to deploy functions after adding or changing scheduled functions.
Changes only take effect after deployment; otherwise, old code runs or no function runs.
Run 'firebase deploy --only functions' every time you update scheduled functions.
Using incorrect cron syntax for scheduling times.
Invalid cron expressions cause the function not to trigger as expected.
Use valid cron syntax like 'every 5 minutes' or '0 9 * * *' and test carefully.
Summary
Initialize Firebase functions with 'firebase init functions' to set up your project.
Write scheduled functions using the 'pubsub.schedule' method to run code on a timer.
Deploy your functions with 'firebase deploy --only functions' to activate them in the cloud.