You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Firebase简易函数遇依赖冲突错误,求解决方案

Firebase云函数部署依赖冲突问题解决方案

问题场景

作为Firebase新手,尝试部署一个用户创建时自动将信息写入Firestore的云函数,代码如下:

const functions = require("firebase-functions");

const admin = require('firebase-admin');

admin.initializeApp();

const db = admin.firestore()

exports.newUser = functions.auth.user().onCreate((user) => {
    return db
        .collection("user")
        .doc(user.uid)
        .create(JSON.parse(JSON.stringify(user)))
})

部署时触发依赖解析错误:

i  functions: updating Node.js 16 function newUser(us-central1)...
Build failed: npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: functions@undefined
npm ERR! Found: firebase-admin@undefined
npm ERR! node_modules/firebase-admin
npm ERR!   firebase-admin@"^111.5.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer firebase-admin@"^10.0.0 || ^11.0.0" from firebase-functions@4.2.1
npm ERR! node_modules/firebase-functions
npm ERR!   firebase-functions@"^4.2.0" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /www-data-home/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /www-data-home/.npm/_logs/2023-02-19T06_03_09_079Z-debug-0.log; Error ID: b0ba1f57

Functions deploy had errors with the following functions:
        newUser(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

问题根源

错误日志明确显示,package.json中firebase-admin的版本被误写为^111.5.0(多打了一个数字1),而当前firebase-functions@4.2.1仅支持firebase-admin@^10.0.0 || ^11.0.0范围内的版本,导致依赖树解析失败。

解决步骤

  • 打开functions目录下的package.json文件
  • 找到dependencies中的firebase-admin条目,将版本号从"^111.5.0"修改为"^11.5.0"(或10.x/11.x范围内的任意稳定版本)
  • 在functions目录下执行npm install重新安装依赖
  • 重新部署云函数:firebase deploy --only functions:newUser

额外优化建议

  • 无需使用JSON.parse(JSON.stringify(user))处理用户对象,直接调用user.toJSON()即可获得可序列化的用户数据,更高效简洁
  • 集合名建议使用复数形式users,符合Firestore的常规命名习惯

优化后的代码示例:

const functions = require("firebase-functions");
const admin = require('firebase-admin');

admin.initializeApp();
const db = admin.firestore()

exports.newUser = functions.auth.user().onCreate((user) => {
    return db
        .collection("users")
        .doc(user.uid)
        .create(user.toJSON())
})

内容的提问来源于stack exchange,提问作者Coder1218

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:56:19