简体   繁体   English

Firestore/Firebase 模拟器未运行

[英]Firestore/Firebase Emulator Not Running

I'm trying to test my functions locally using the guide listed here https://firebase.google.com/docs/functions/local-emulator我正在尝试使用此处列出的指南https://firebase.google.com/docs/functions/local-emulator在本地测试我的功能

I have installed the latest firebase-tools using我已经安装了最新的 firebase-tools 使用

npm install -g firebase-tools

In my package.json I confirmed to be running在我的package.json我确认正在运行

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

When I try to run my functions using当我尝试使用

firebase emulators:start

It gives me the below output.它给了我下面的 output。 What am I doing wrong?我究竟做错了什么?

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\"."} 

I had the same issue there were a few things wrong for me我遇到了同样的问题,我有一些问题

  1. ensure the emulator is installed by running firebase setup:emulators:firestore通过运行firebase setup:emulators:firestore确保安装了firebase setup:emulators:firestore

My second issue was that my initial firebase configuration had installed the config files into my home folder rather then the project folder as described [here] this meant so my project was missing firestore.rules and firestore.indexes.json and some of the configuration settings.我的第二个问题是,我的初始 firebase 配置已将配置文件安装到我的主文件夹中,而不是[此处]所述的项目文件夹中,这意味着我的项目缺少 firestore.rules 和 firestore.indexes.json 以及一些配置设置.

run firebase init to generate these files运行 firebase init 来生成这些文件

Once I fixed these two things it worked for me.一旦我解决了这两件事,它就对我有用。 I hope this helps.我希望这有帮助。

As a reference my firebase.json looks like this作为参考,我的 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"
    }
  }
}

If checking Firebase setup doesn't work, try this:如果检查 Firebase 设置不起作用,请尝试以下操作:

  • Run firebase emulators:start .运行firebase emulators:start Check if displayed error request to install OpenJDK.检查是否显示安装 OpenJDK 的错误请求。
  • If your functions interacts with Firebase APIs or Google APIs, you need to setup admin credentials.如果您的函数与 Firebase API 或 Google API 交互,则需要设置管理员凭据。 Check how to do it here: https://firebase.google.com/docs/functions/local-emulator在此处检查如何操作: https : //firebase.google.com/docs/functions/local-emulator
  • You may need to emulate functions and firestore at the same time.您可能需要同时模拟函数和 firestore。 Use firebase emulators:start --only functions,firestore or firebase serve --only functions,firestore .使用firebase emulators:start --only functions,firestorefirebase serve --only functions,firestore
  • Keep in mind that pubsub is not suported yet.请记住,尚未支持 pubsub。 As Sam Stern comments, pub sub is now supported.正如 Sam Stern 评论的那样,现在支持 pub sub。

Could be you don't have firestore configured properly in your firebase.json file.可能是您的 firebase.json 文件中没有正确配置 firestore。 This makes emulator not being started.这使得模拟器无法启动。

What you need is to run firebase init firestore in your project directory.你需要的是在你的项目目录中运行firebase init firestore This would create firestore rules and indexes files and update your firebase.json correspondingly.这将创建 firestore 规则和索引文件,并相应地更新您的 firebase.json。

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

Actually this error comes when the user initialize the firebase project without database.实际上,当用户在没有数据库的情况下初始化 firebase 项目时,就会出现此错误。 So the command firebase emulators:start --only database can not start the database emulator because it need the "database.rules.json" file and the configuration entries for database in the firebase.json file .所以命令firebase emulators:start --only database无法启动数据库模拟器,因为它需要“database.rules.json”文件和 firebase.json 文件中的数据库配置条目。 So if you forgot to initialize the database in firebase init command then you can add firebase database whenever you want by following firebase CLI command因此,如果您忘记在firebase init命令中初始化数据库,那么您可以随时按照 firebase CLI 命令添加 firebase 数据库

firebase init database

then you can run firebase emulators:start --only database for starting the database emulator in the localserver .然后您可以运行firebase emulators:start --only database以在firebase emulators:start --only database中启动数据库模拟器。

and If you want to use emulator for both function and database then run firebase serve --only functions,database并且如果您想对函数和数据库使用模拟器,请运行firebase serve --only functions,database

Simple fix简单的修复

  1. Check you have latest firebase-tools (8.x)检查您是否拥有最新的firebase-tools (8.x)
  2. Add an empty firestore configuration to firebase.jsonfirebase.json添加一个空的Firestore配置
{
  "functions": {
    ...
  },
  "firestore": {}
}

This will tell firebase-tools to initialize and run the firestore emulator.这将告诉firebase-tools初始化并运行 firestore 模拟器。

在此处输入图片说明

For me after installing th java runtime , everything worked perfectly.对我来说,在安装了 java 运行时之后,一切都运行良好。

install java runtime安装 Java 运行时

Also came across this issue, I was importing data, but I was in the wrong directory, simple fix.也遇到了这个问题,我正在导入数据,但是我在错误的目录中,简单修复。 Hope this may help someone as error output gives no indication.希望这可以帮助某人,因为错误输出没有给出任何指示。

Original command:原始命令:

firebase emulators:start --import ./firebaseexport

Error:错误:

i  emulators: Starting emulators: functions, firestore, hosting
i  emulators: Shutting down emulators.
i  hub: Stopping emulator hub

Error: An unexpected error has occurred.

Fix:使固定:

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

In the firebase.json file Change host for firestore from在 firebase.json 文件中,将 Firestore 的主机更改为

"host": "http://localhost"

(which is created during the firebase init command) to (在 firebase init 命令期间创建)到

"host": "localhost"

Doing this fixes the problem and the firestore emulator runs.这样做可以解决问题,并且 firestore 模拟器会运行。

Elaboration: firebase version 9.16.0详细说明:firebase 版本 9.16.0

With the firebase.json snippet使用 firebase.json 片段

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

The emulator would stop with:模拟器将停止:

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.

But when "http:localhost" is changed to "localhost", then it works (the error message is missleading)..但是当“http:localhost”更改为“localhost”时,它就可以工作了(错误消息是误导性的)。

The key was (in debug mode) the line关键是(在调试模式下)这条线

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

one suggestion in another link was to check /etc/hosts, but a simplier fix was to get rid of the "http://" part.另一个链接中的一个建议是检查 /etc/hosts,但更简单的解决方法是去掉“http://”部分。

I fixed it by doing我通过这样做来修复它

  1. firebase init firebase 初始化

select the things you need using space key Then after finish select 使用空格键需要的东西 然后完成后

  1. firebase emulators:start firebase 仿真器:开始

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

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