Firebase模拟器启动正常,访问Hosting模拟器时抛出EBADF错误
本地运行firebase emulators:start后所有组件加载正常,将Express应用结合Cloud Functions与Firebase Hosting部署至Firebase后,本地代码与部署版本完全一致,但访问模拟器中心正常,加载Hosting时抛出如下错误:
Error: EBADF: bad file descriptor, uv_pipe_open
at Object._forkChild (node:child_process:175:5)
at setupChildProcessIpcChannel (node:internal/bootstrap/pre_execution:451:30)
at prepareMainThreadExecution (node:internal/bootstrap/pre_execution:71:3)
at node:internal/main/run_main_module:9:1 {
errno: -4083,
code: 'EBADF',
syscall: 'uv_pipe_open'
}
我的firebase.json内容:
{ "hosting": { "rewrites": [ { "source": "**", "function": "app" } ], "public": "static", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ] }, "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "functions": [ { "runtime": "nodejs16", "source": "/functions", "codebase": "default", "ignore": [ "node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log" ] } ], "storage": { "rules": "storage.rules" }, "emulators": { "auth": { "port": 9099 }, "functions": { "port": 5001 }, "firestore": { "port": 8080 }, "hosting": { "port": 5000 }, "storage": { "port": 9199 }, "ui": { "enabled": true }, "singleProjectMode": true } }
文件目录结构:
├── firebase.json ├── firestore-debug.log ├── firestore.indexes.json ├── firestore.rules ├── functions │ ├── firebase-debug.log │ ├── firestore-debug.log │ ├── index.js │ ├── index.pug │ ├── package-lock.json │ ├── package.json │ └── ui-debug.log ├── index.html ├── main.js ├── main.pug ├── package-lock.json ├── package.json ├── static │ ├── android-chrome-192x192.png │ ├── apple-touch-icon.png │ ├── browserconfig.xml │ ├── css │ │ └── styles.css │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ ├── favicon.ico │ ├── js │ │ ├── firebaseComponents.js │ │ └── getUserState.js │ ├── mstile-150x150.png │ ├── safari-pinned-tab.svg │ ├── site.webmanifest │ ├── tapewinder_draft1.png │ ├── tapewinder_draft3.png │ ├── tapewinder_placeholder.png │ ├── tapewinder_placeholder_blue.png │ ├── tapewinder_placeholder_green.png │ ├── tapewinder_placeholder_orange.png │ ├── tapewinder_placeholder_pink.png │ ├── tapewinder_placeholder_raisin.png │ ├── tapewinder_placeholder_red.png │ └── tapewinder_whitebg.png ├── storage.rules └── ui-debug.log
已暂存上次部署至Firebase后的所有修改,也调整过firebase.json,但问题仍未解决。
可能的解决方法
1. 修正Functions的source路径
firebase.json中Functions的source字段写了绝对路径"/functions",改成相对路径"functions"(去掉开头斜杠),绝对路径可能导致模拟器无法正确定位函数代码,引发进程启动异常。修改后配置如下:
"functions": [ { "runtime": "nodejs16", "source": "functions", "codebase": "default", "ignore": [ "node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log" ] } ]
2. 清理模拟器缓存
删除项目根目录和Functions目录下的所有*-debug.log文件,然后执行:
firebase emulators:start --clear-cache
缓存文件损坏可能导致子进程IPC通道异常。
3. 重新安装Functions依赖
进入Functions目录,删除node_modules和package-lock.json后重新安装依赖:
cd functions rm -rf node_modules package-lock.json npm install
依赖包损坏可能导致函数进程启动失败,进而引发Hosting重写时的IPC错误。
4. 检查Node.js版本兼容性
当前使用的是nodejs16 runtime,确认本地Node.js版本与该runtime一致,或尝试升级到Node.js 18(同步修改firebase.json中的runtime配置),低版本Node.js在模拟器fork子进程时可能出现文件描述符问题。
5. 排查端口占用
执行命令检查Hosting(5000)和Functions(5001)端口是否被其他进程占用:
- Windows:
netstat -ano | findstr :5000 netstat -ano | findstr :5001
- Linux/macOS:
lsof -i :5000 lsof -i :5001
如果有占用进程,杀掉后重新启动模拟器。
内容的提问来源于stack exchange,提问作者user14695006

