部署Firebase Cloud Functions时出现npm依赖缺失错误
Firebase Functions部署报错:缺失npm依赖包(error-ex@1.3.2等)
问题描述
初始化新Firebase项目并添加Functions组件后,尝试部署默认helloWorld函数:
const functions = require("firebase-functions"); // // Create and deploy your first functions // // https://firebase.google.com/docs/functions/get-started // exports.helloWorld = functions.https.onRequest((request, response) => { functions.logger.info("Hello logs!", {structuredData: true}); response.send("Hello from Firebase!"); });
部署时触发错误,提示大量npm包缺失,部分错误日志如下:
i functions: updating Node.js 16 function helloWorld(us-central1)... Build failed: ...c-fn@2.1.0 from lock file npm ERR! Missing: error-ex@1.3.2 from lock file ... Functions deploy had errors with the following functions: helloWorld(us-central1)
已尝试的操作均未解决问题:
- 手动安装firebase-functions包
- 初始化时让Firebase自动安装依赖
- 删除node_modules、清除npm缓存、移除锁文件后重新安装
- 重新初始化Firebase项目
- 更换Node.js版本(目标版本为16)
可行修复方案
强制重新生成完整依赖锁文件
进入functions目录执行:rm -rf node_modules package-lock.json yarn.lock npm cache clean --force npm install --force--force参数会强制npm重新解析所有依赖,避免缓存导致的依赖缺失。升级Firebase CLI到最新版本
旧版CLI可能存在依赖解析bug,执行升级命令:npm install -g firebase-tools@latest升级完成后重新部署。
明确指定Node.js版本
在functions目录的package.json中固定Node.js版本,和部署环境保持一致:"engines": { "node": "16" }避免使用
^16这类带前缀的写法,防止本地与云端版本差异。切换到稳定的npm源
若使用非官方npm源,切换回官方源尝试:npm config set registry https://registry.npmjs.org/国内用户可使用同步正常的镜像源:
npm config set registry https://registry.npmmirror.com/手动安装缺失的依赖包
针对错误日志中明确提到的缺失包(如error-ex@1.3.2),手动安装:npm install error-ex@1.3.2 --save若还有其他缺失包,重复此操作后重新部署。
内容的提问来源于stack exchange,提问作者Laurens Smid
相关产品推荐
相关产品推荐

