繁体   English   中英

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

它给了我下面的 output。 我究竟做错了什么?

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. 通过运行firebase setup:emulators:firestore确保安装了firebase setup:emulators:firestore

我的第二个问题是,我的初始 firebase 配置已将配置文件安装到我的主文件夹中,而不是[此处]所述的项目文件夹中,这意味着我的项目缺少 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 设置不起作用,请尝试以下操作:

  • 运行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.json 文件中没有正确配置 firestore。 这使得模拟器无法启动。

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

从 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 emulators:start --only database中启动数据库模拟器。

并且如果您想对函数和数据库使用模拟器,请运行firebase serve --only functions,database

简单的修复

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

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

在此处输入图片说明

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

安装 Java 运行时

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

原始命令:

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

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

我通过这样做来修复它

  1. firebase 初始化

select 使用空格键需要的东西 然后完成后

  1. firebase 仿真器:开始

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM