Firestore/Firebase 模拟器不是 运行

Firestore/Firebase Emulator Not Running

我正在尝试使用此处列出的指南在本地测试我的功能 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\"."} 

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

  1. 确保模拟器已由 运行ning 安装 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.json 文件中正确配置 firestore。这使得模拟器无法启动。

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

如果检查 Firebase 设置不起作用,试试这个:

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

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

实际上这个错误是在用户初始化没有数据库的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

简单修复

  1. 检查你有最新的 firebase-tools (8.x)
  2. 将空 firestore 配置添加到 firebase.json
{
  "functions": {
    ...
  },
  "firestore": {}
}

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

对我来说,在安装 java 运行时后,一切正常。

install java runtime

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

原命令:

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

在 firebase.json 文件中从

更改 firestore 的主机
"host": "http://localhost"

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

"host": "localhost"

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

阐述: Firebase 版本 9.16.0

使用 firebase.json 片段

{
        // ...
        "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

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