【问题标题】:Running Cloud Functions locally gives error "functions.config() is not available"在本地运行 Cloud Functions 会出现错误“functions.config() 不可用”
【发布时间】:2017-08-08 01:30:18
【问题描述】:

Cloud Functions for Firebase 刚刚发布,我正在按照说明进行全新安装。 Here is the "Get Started" page.

我已经安装了“npm install -g firebase-tools”并且我的所有文件都在我的项目中。我正在使用 WebStorm 2016.3 和 Node.JS v6.10.0。

我还安装并设置了 firebase login 和 firebase init 功能。 我的设置。

我的 package.json

 {
   "name": "functions",
   "description": "Cloud Functions for Firebase",
    "dependencies": {
     "firebase-admin": "^4.1.2",
     "firebase-functions": "^0.5"
   },
    "private": true
 }

前两行代码有效。

  const functions = require('firebase-functions');
  const admin = require('firebase-admin');

但是当我尝试运行这条线时......

  admin.initializeApp(functions.config().firebase);

我收到此错误。

 Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function.
   at init (/Users/.../functions/node_modules/firebase-functions/lib/config.js:46:19)
   at Object.config (/Users/.../functions/node_modules/firebase-functions/lib/config.js:29:9)
   at Object.<anonymous> (/Users/.../functions/index.js:11:31)
   at Module._compile (module.js:570:32)
   at Object.Module._extensions..js (module.js:579:10)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.runMain (module.js:604:10)
   at run (bootstrap_node.js:394:7)

我在这里错过了什么?

【问题讨论】:

  • 2年后,这仍然是一个没有简单解决方案的问题。下面没有任何东西对我有用,任何其他线程也没有。
  • @Ryan 你在尝试运行 Firebase Cloud Functions 吗?
  • 是的。 Firebase 函数
  • @Ryan 与 FCF 基本上你只是在编写将在 Firebase 服务器上运行的代码,而不是你的本地机器。所有控制台日志都在 Firebase 控制台中。不确定这是否是您正在寻找的。​​span>
  • 我明白这一点。我是说在本地模拟这些功能仍然存在问题——因为它从未在多个项目和机器上对我有用。它需要部署来测试任何东西。

标签: node.js firebase google-cloud-functions


【解决方案1】:

看起来您可能仍然拥有旧版本的 firebase CLI,即使您尝试安装最新的 firebase-tools

您可以通过运行firebase --version 进行检查。至少应该说3.5.0。如果没有,您将需要再次运行 npm install -g firebase-tools,这有望解决问题。

如果您的firebase --version 继续显示错误的版本,您需要检查是否不小心安装了多个版本的firebase-tools

【讨论】:

  • 是的,我有 3.5.0。我会环顾四周,看看是否可以卸载 Firebase 并重新安装。另外,我的 firebase.json 文件是空白的,对吗?
  • 我不希望该文件为空白。即使firebase.json 中没有值,它也会包含{}。您可能想再次尝试运行firebase init
  • 对我来说没有用。我需要手动编写 config.json 文件。在此处查看方法:github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/…
  • 但是 npm 上的 firebasefirebase-tools 是两个不同的东西。例如firebase-tools 最新版本是 3.17.4 - 那怎么样?
  • 刚刚使用npm list -g查看了我有哪些全局包,并整理出最新的firebase-toolsfirebase包是2.4.2版...
【解决方案2】:

如果像我一样,您在尝试在本地运行函数时遇到此错误,那是因为 functions.config() 仅在 Cloud Functions 运行时中可用。

如果您在部署之前尝试测试您的功能,这里是有关如何测试的文档的链接:run functions locally。具体来说,这部分很有趣:

如果您使用自定义函数配置变量,请在运行 firebase serve 之前在项目的函数目录中运行以下命令。

firebase functions:config:get > .runtimeconfig.json

但是,如果您使用的是 Windows PowerShell,请将上述命令替换为:

firebase functions:config:get | ac .runtimeconfig.json

【讨论】:

  • 对我来说,它使用了其他环境变量。我在相邻页面上找到了答案,它说要创建一个复制实际配置文件的本地运行时配置,因为该配置仅适用于已部署的代码。对于本地,运行此firebase functions:config:get &gt; .runtimeconfig.json
  • 这帮助我弄清楚如何在部署之前进行本地测试:firebase.google.com/docs/functions/local-emulator
  • @MartavisP。你救了我的一天!重要提示:.runtimeconfig.json 应该放在“functions”文件夹中,因此也有问题,因为最初放置在根目录下。
  • 一年后我又回到了同一个地方。再次感谢。
【解决方案3】:

我从错误的目录运行firebase deploy。希望这可以节省一些时间。

【讨论】:

  • 您应该从哪个目录部署?
  • 无论您的 firebase.json 或 .firebaserc 位于何处。
【解决方案4】:

在我的情况下 .runtimeconfig.json / .env / .env.local 不起作用 - 在任何文件夹中。

事实证明,windows 上与 PWD 相关的 firebase-tools 存在一些问题。

在你执行“firebase emulators:start”之前,运行这个:

$env:CLOUD_RUNTIME_CONFIG="$(pwd)/functions/.runtimeconfig.json"

【讨论】:

  • 这太奇怪了。这不是firebase-tools@9.16.0 的问题,但是一旦我升级到firebase-tools@10.0.1.runtimeconfig.json 不会自动加载,除非我使用您的命令明确设置CLOUD_RUNTIME_CONFIG
  • 我在这里报告了这个问题:github.com/firebase/firebase-tools/issues/3983
猜你喜欢
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 2018-10-10
  • 2020-07-28
相关资源
最近更新 更多