使用 Google Cloud Task 客户端部署 Firebase Functions 时收到区域错误

Receiving a region error deploying Firebase Functions with Google Cloud Task client

问题

当我部署我的 Firebase Functions 导入 Google Cloud Tasks @google-cloud/tasks 我收到一个区域错误。

为了证明这一点,我包含了提供成功和不成功部署的代码。

成功

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!");
});

未成功

CloudTaskClientonDeletePostCancelTask函数添加到成功代码中

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: 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

在 Firebase Functions 日志中,我发现了以下内容:

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 
...

更多信息

{
  "dependencies": {
    "@google-cloud/tasks": "^2.4.2",
    "firebase-admin": "^10.0.0",
    "firebase-functions": "^3.16.0"
  }
}

函数部署失败,因为在你的函数目录下找不到package.json中的@google-cloud/tasks,区域关注没有问题。如果您的函数中缺少依赖项,则无法将其部署到任何区域。

要修复它并在 function/package.json 中添加 @google-cloud/tasks 依赖项,运行 在 function 目录中执行以下命令:

npm install @google-cloud/tasks

再次部署,应该会成功。