首页 > 解决方案 > 如何在 Node 6 上构建模拟器但 Node 6 不推荐使用 Functions 时在本地运行 Google Cloud Functions?

问题描述

Google Cloud Functions 模拟器仅在节点 6 上受支持: https ://github.com/GoogleCloudPlatform/cloud-functions-emulator

注意:模拟器仅支持 Node v6.xx,不支持 Node v8.xx 或 Python。”

但是,节点 6 不推荐使用 Google Cloud Functions: https ://cloud.google.com/functions/docs/concepts/nodejs-6-runtime

Node.js 6 运行时已被弃用。为确保您的函数在受支持的 Node.js 版本上,请将它们迁移到 Node.js 8 或 Node.js 10。2020-04-22 之后,使用 Node.js 6 的函数部署将被阻止。在此时间之后继续使用 Node.js 6 的 Cloud Functions 可能会被禁用。”

如何在本地运行函数(为 Node 8 编写)?

标签: google-cloud-functions

解决方案


谷歌(2019 年 4 月)刚刚发布了一个新的 Cloud Functions 测试和开发框架,用于基于 Node.js 的逻辑。这个框架的名字叫做Functions Framework for Node.js并且在 Github 上开源。如果您访问刚刚提供的链接,您将找到有关如何下载和使用它的详细文档。在最高(和摘要)级别:

  1. npm install @google-cloud/functions-framework

  2. 编写你的代码。

exports.helloWorld = (req, res) => {
  res.send('Hello, World');
};
  1. 使用运行您的代码npx @google-cloud/functions-framework --target=helloWorld

虽然这些是备忘单说明,但我强烈建议您阅读 Github 存储库中的整个 README 文档。


推荐阅读