Home > Software design >  Receiving a region error deploying Firebase Functions with Google Cloud Task client
Receiving a region error deploying Firebase Functions with Google Cloud Task client

Time:11-18

Problem

When I deploy my Firebase Functions importing Google Cloud Tasks @google-cloud/tasks I receive a region errror.

To demonstrate this I include the code giving successful and unsuccessful deployment.

Successfull

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

export const helloWorld = functions.region("europe-west3").https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

Unsuccessful

CloudTaskClient and onDeletePostCancelTask function are added to the successful code.

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
const {CloudTasksClient} = require("@google-cloud/tasks");

admin.initializeApp();
const tasksClient = new CloudTasksClient();

export const helloWorld = functions.region("europe-west3").https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

export const onDeletePostCancelTask = functions.region("europe-west3").database
    .ref("/one/{twoId}").onDelete(async (snapshot, context) => {
      const dogId = snapshot.key;
      const taskSnap = await snapshot.ref.parent?.parent?.child("three/"   twoId).get();
      const taskName = taskSnap?.val();
      console.log("Task name: ", taskName);
      return tasksClient.deleteTask({name: taskName});
    });

Error:

Error: There was an error deploying functions:
- Error Failed to create function helloWorld in region europe-west3
- Error Failed to create function onDeletePostCancelTask in region europe-west3

In Firebase Functions log I found the following:

Provided module can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '@google-cloud/tasks'
Require stack: 
- /workspace/lib/index.js 
...

More Info

  • I was following the installation process as described in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
  • My Firebase app region is europe-west3.
  • My Google Cloud app region is europe-west. (However, at the beggigning configuring the Cloud Tasks I created new project with us-central1 region, but since there was an instance of my project with europe-west region I switched to it.)
  • When I omit .region("europe-west3") the error stays, just with us-central1 instead.
  • I have two package.json, first in ../ the second in ../functions/. The outer one has the @google-cloud/tasks dependency :
{
  "dependencies": {
    "@google-cloud/tasks": "^2.4.2",
    "firebase-admin": "^10.0.0",
    "firebase-functions": "^3.16.0"
  }
}

CodePudding user response:

The function deployment failed because it cannot find the @google-cloud/tasks in package.json in your function directory and there's no problem about region concern. It can't be deployed to any region if there's a missing dependency in your function.

To fix it and add the @google-cloud/tasks dependency in your function/package.json, run the following command in your function directory:

npm install @google-cloud/tasks

Deploy again, and it should be successful.

  • Related