【发布时间】:2020-12-18 15:02:05
【问题描述】:
我正在尝试在本地环境中运行 firebase cloud functions 模拟器,但遇到了 EADDRINUSE 问题。
- 我可以成功运行 firebase 模拟器:
✔ functions[main]: http function initialized (http://localhost:5001/firebaseapp/main).
┌───────────────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! View status and logs at http://localhost:4000 │
└───────────────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
└───────────┴────────────────┴─────────────────────────────────┘
Other reserved ports: 4400, 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
> Server Started
> Connected to MongoDB
- 但是,当我使用模拟器提供的链接时,事情并没有按预期进行:
http://localhost:5001/firebaseapp/main
提出我的测试请求:
http://localhost:5001/firebaseapp/main/test
我收到以下错误:
> events.js:288
> throw er; // Unhandled 'error' event
> ^
>
> Error: listen EADDRINUSE: address already in use :::5000
> at Server.setupListenHandle [as _listen2] (net.js:1309:16)
> at listenInCluster (net.js:1357:12)
> at Server.listen (net.js:1445:7)
> at Function.listen (/Users/Desktop/fb_tour/functions/node_modules/express/lib/application.js:618:24)
> at Object.<anonymous> (/Users/Desktop/fb_tour/functions/index.js:61:5)
> at Module._compile (internal/modules/cjs/loader.js:1158:30)
> at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
> at Module.load (internal/modules/cjs/loader.js:1002:32)
> at Function.Module._load (internal/modules/cjs/loader.js:901:14)
> at Module.require (internal/modules/cjs/loader.js:1044:19)
> at require (internal/modules/cjs/helpers.js:77:18)
> at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:681:33
> at Generator.next (<anonymous>)
> at fulfilled (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:5:58)
> at processTicksAndRejections (internal/process/task_queues.js:97:5)
> Emitted 'error' event on Server instance at:
> at emitErrorNT (net.js:1336:8)
> at processTicksAndRejections (internal/process/task_queues.js:84:21) {
> code: 'EADDRINUSE',
> errno: 'EADDRINUSE',
> syscall: 'listen',
> address: '::',
> port: 5000
> }
我试图杀死端口 5000,但显然没有活动的 PID 进程,所以我无法获得 PID。我还重新启动了计算机以强制关闭每个进程,但此错误仍然存在。
有什么建议吗?
编辑:即使我在我的 nodejs 应用程序中更改侦听端口,我也遇到了同样的问题。例如当我设置端口 3000 时,我收到相同的错误消息,告知端口 3000 已在使用中。
编辑 2:我想我已经确定了问题的原因。运行函数模拟器时出现此警告消息
It seems that you are running multiple instances of the emulator suite for project firebaseapp. This may result in unexpected behavior.
我认为我的应用程序有两个实例同时运行,导致此错误。但是我不知道如何解决这个问题。
【问题讨论】:
-
如果您有包含特定复制说明的 Firebase CLI 错误报告,请在 GitHub 上提交错误报告。 github.com/firebase/firebase-tools
-
尝试杀死 5001 上的进程
-
我一直这样做,在开发过程中,如果代码中出现未处理的异常,他们可能会在中间抛出错误,我会在地址已在使用错误后杀死 5001
-
@vinoth10 正如我在编辑中所说,我刚刚找到了导致此问题的原因。我收到此警告消息
It seems that you are running multiple instances of the emulator suite for project firebaseapp. This may result in unexpected behavior.,但我不知道为什么我的应用程序上的多个实例同时运行。 -
我之前在编写云函数时遇到过这个问题。首先,您不需要传递所有这些详细信息来启动管理应用程序。我所做的只是将
import * as admin from 'firebase-admin'; admin.initializeApp();作为 index.ts/index.js 中的前 2 行,然后 import * as admin from 'firebase-admin'; const db = admin.firestore();在各自的云功能中
标签: node.js firebase express google-cloud-functions firebase-cli