使用Express的Google Cloud Function报错:TypeError: stream.listeners is not a function
解决Google Cloud Function中
TypeError: stream.listeners is not a function错误 这个错误的核心原因是你使用的express@4.17.1与firebase-functions@4.8.1的依赖环境不兼容。具体来说,express依赖的finalhandler库调用了unpipe库的方法,而该方法在当前云函数运行环境中尝试访问的stream对象不存在listeners()方法,导致报错。
方案1:升级express到兼容版本
将express版本升级到^4.18.2(该版本修复了相关依赖的兼容性问题),修改后的package.json如下:
{ "dependencies": { "firebase-functions": "^4.8.1", "express": "^4.18.2" } }
修改后重新安装依赖,你的原有代码可以正常运行。
方案2:移除express,直接使用云函数原生处理
对于这种简单的单路由场景,没必要引入express,直接使用Google Cloud Function的原生HTTP处理逻辑即可,代码更轻量且避免依赖冲突:
const functions = require('firebase-functions'); exports.googleLogin = functions.https.onRequest((req, res) => { // 限制仅POST请求访问 if (req.method !== 'POST') { return res.status(405).send('Method Not Allowed'); } res.send('Auth successful'); });
验证方法
修改后,重新部署云函数即可验证是否解决问题:
firebase deploy --only functions:googleLogin
内容的提问来源于stack exchange,提问作者elena
相关产品推荐
相关产品推荐

