【问题标题】:Firestore/Firebase Emulator Not RunningFirestore/Firebase 模拟器未运行
【发布时间】:2019-10-03 02:37:39
【问题描述】:

我正在尝试使用此处列出的指南在本地测试我的功能 https://firebase.google.com/docs/functions/local-emulator

我已经使用

安装了最新的 firebase-tools

npm install -g firebase-tools

在我的package.json 中,我确认正在运行

"firebase-admin": "^7.3.0",
"firebase-functions": "^2.3.1",

当我尝试使用

运行我的函数时

firebase emulators:start

它给了我下面的输出。 我做错了什么?

Starting emulators: ["functions"]
⚠  Your requested "node" version "8" doesn't match your global version "11"
✔  functions: Emulator started at http://localhost:5001
i  functions: Watching "[FUNCTIONS FOLDER PATH]" for Cloud Functions...
⚠  Default "firebase-admin" instance created!
⚠  Ignoring trigger "[FUNCTION NAME]" because the service "firebaseauth.googleapis.com" is not yet supported.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.
⚠  Ignoring trigger "[FUNCTION NAME]" because the Cloud Firestore emulator is not running.

etc.
etc.
etc.
i  functions: HTTP trigger initialized at http://localhost:5001/[APP NAME]/us-central1/[FUNCTION NAME]

[2019-05-15T21:43:52.436Z]  @firebase/database: FIREBASE WARNING:  
{"code":"app/invalid-credential","message":"Credential implementation provided to   
initializeApp() via the \"credential\" property failed to fetch a valid Google  
OAuth2 access token with the following error: \"Error fetching access token: Error  
while making request: getaddrinfo ENOTFOUND metadata.google.internal  
metadata.google.internal:80. Error code: ENOTFOUND\"."} 

【问题讨论】:

  • 请限制自己在每个帖子中回答一个问题。就目前而言,您正在询问两个不相关的事情。如果您在模拟器中有看起来像错误的东西的复制步骤,请发布项目 GitHub 而不是 Stack Overflow。 github.com/firebase/firebase-tools
  • 删除了“不相关的东西”,即使它是输出中显示的警告消息的一部分.....
  • 而且我不知道这是否是一个错误,这就是道格提出这个问题的目的。除了firebase emulators:start 的单个命令行代码之外,没有任何复制步骤,这是指南所说的唯一要做的事情。如果 Firebase 改进了这方面的文档,那就太好了
  • 复制步骤必然会说明人们可以做的一切来达到这种状态,包括显示正在使用的任何代码。最好是触发问题的最少代码量,因此人们不必尝试处理不必要的代码。与 Stack Overflow 相比,制作模拟器的工程师更可能关注 GitHub 问题。
  • 我有 37 个功能在部署到云环境时可以完美运行。当使用上述指南在本地运行时,如上所示,所有错误都会被炸毁。将尝试项目github

标签: firebase google-cloud-firestore google-cloud-functions firebase-cli


【解决方案1】:

在 firebase.json 文件中将 firestore 的主机从

"host": "http://localhost"

(在 firebase init 命令期间创建) 到

"host": "localhost"

这样做可以解决问题并运行 Firestore 模拟器。

阐述: firebase 版本 9.16.0

使用 firebase.json sn-p

{
        // ...
        "emulators": {
          "firestore": {
            "host": "http://localhost",
            "port": "8081"
          }
        }
      }

模拟器会停止:

firebase emulators:start --only firestore
i  emulators: Starting emulators: firestore
i  emulators: Shutting down emulators.
i  hub: Stopping emulator hub
⚠  firestore: Port 8081 is not open on http://localhost, could not start Firestore Emulator.
⚠  firestore: To select a different host/port, specify that host/port in a firebase.json config file:
      {
        // ...
        "emulators": {
          "firestore": {
            "host": "HOST",
            "port": "PORT"
          }
        }
      }
i  emulators: Shutting down emulators.

Error: Could not start Firestore Emulator, port taken.

但是当“http:localhost”改为“localhost”时,就可以了 (错误信息具有误导性)..

关键是(在调试模式下)行

port check error: Error: getaddrinfo ENOTFOUND http://localhost

另一个链接中的一个建议是检查 /etc/hosts,但更简单的解决方法是去掉“http://”部分。

【讨论】:

    【解决方案2】:

    也遇到了这个问题,我在导入数据,但是我在错误的目录下,简单修复。希望这可以帮助某人,因为错误输出没有给出任何指示。

    原始命令:

    firebase emulators:start --import ./firebaseexport
    

    错误:

    i  emulators: Starting emulators: functions, firestore, hosting
    i  emulators: Shutting down emulators.
    i  hub: Stopping emulator hub
    
    Error: An unexpected error has occurred.
    

    修复:

    firebase emulators:start --import ./functions/firebaseexport
    

    【讨论】:

      【解决方案3】:

      对我来说,安装 java 运行时后,一切都运行良好。

      install java runtime

      【讨论】:

        【解决方案4】:

        简单修复

        1. 检查您是否有最新的firebase-tools (8.x)
        2. 向firebase.json 添加一个空的firestore 配置
        {
          "functions": {
            ...
          },
          "firestore": {}
        }
        

        这将告诉firebase-tools 初始化并运行firestore 模拟器。

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,但我遇到了一些问题

          1. 通过运行确保模拟器已安装 firebase setup:emulators:firestore

          我的第二个问题是我最初的 firebase 配置已将配置文件安装到我的主文件夹中,而不是像 [here] 描述的项目文件夹中,这意味着我的项目缺少 firestore.rules 和 firestore.indexes.json 以及一些配置设置。

          运行 firebase init 来生成这些文件

          一旦我解决了这两个问题,它就对我有用。我希望这会有所帮助。

          作为参考,我的 firebase.json 看起来像这样

          {
            "functions": {
              "predeploy": [
                "npm --prefix \"$RESOURCE_DIR\" run lint",
                "npm --prefix \"$RESOURCE_DIR\" run build"
              ]
            },
            "firestore": {
              "rules": "firestore.rules",
              "indexes": "firestore.indexes.json"
            },
            "hosting": {
              "public": "dist",
              "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
              "rewrites": [
                {
                  "source": "**",
                  "destination": "/index.html"
                }
              ]
            },
            "emulators": {
              "firestore": {
                "port": "5002"
              }
            }
          }
          

          【讨论】:

          • 运行firebase init 确实解决了这个问题。另一方面,我刚刚为函数创建了一个新文件夹并运行了 init。奇怪的是我不得不再次运行它,firebase 不知道我最初使用的是 firestore 和函数本身吗? :-/
          • 同意@AswinKumar
          • firebase init 不适合我。 firebase init firestore 解决我的问题。
          【解决方案6】:

          实际上,当用户在没有数据库的情况下初始化 firebase 项目时会出现此错误。 因此命令firebase emulators:start --only database 无法启动数据库模拟器,因为它需要“database.rules.json”文件和firebase.json 文件中的数据库配置条目。 因此,如果您忘记在 firebase init 命令中初始化数据库,那么您可以随时按照 firebase CLI 命令添加 firebase 数据库

          firebase init database

          然后你可以运行 firebase emulators:start --only database 用于在本地服务器中启动数据库模拟器。

          如果你想对函数和数据库都使用模拟器,那么运行 firebase serve --only functions,database

          【讨论】:

            【解决方案7】:

            如果检查 Firebase 设置不起作用,请尝试以下操作:

            • 运行firebase emulators:start。检查是否显示安装 OpenJDK 的错误请求。
            • 如果您的函数与 Firebase API 或 Google API 交互,您需要设置管理员凭据。在此处查看操作方法:https://firebase.google.com/docs/functions/local-emulator
            • 您可能需要同时模拟函数和 Firestore。使用firebase emulators:start --only functions,firestore 或firebase serve --only functions,firestore。
            • 请记住,目前还不支持 pubsub。 作为 Sam Stern cmets,现在支持 pub sub。

            【讨论】:

            • 其实pubsub 最新版本支持(7.9.0)
            • 我收到错误消息:functions firestore 不是有效的模拟器名称,有效的选项是:["auth","functions","firestore","database","hosting","pubsub "] 使用 firebase 模拟器时:start --only functions,firestore
            【解决方案8】:

            从 Firebase CLI (firebase-tools) 的 7.8.0 版本开始,有一个新命令 firebase init emulators 将帮助您设置要运行的所有模拟器。

            【讨论】:

            • 这是关键!感谢山姆指出。看起来我没有找到任何关于这个的地方。如果没有添加,如果您可以添加到文档中,那就太好了。
            【解决方案9】:

            可能是您的 firebase.json 文件中没有正确配置 firestore。这使得模拟器无法启动。

            您需要在项目目录中运行firebase init firestore。这将创建 firestore 规则和索引文件并相应地更新您的 firebase.json。

            【讨论】:

            • 我将firebase init database 用于实时数据库
            猜你喜欢
            • 2019-11-23
            • 1970-01-01
            • 1970-01-01
            • 2022-10-14
            • 1970-01-01
            • 2020-03-14
            • 1970-01-01
            • 2021-05-16
            • 1970-01-01
            相关资源
            最近更新 更多