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

运行Firebase Firestore测试时遇TypeError: module.exports.v1 is not a constructor错误

解决Firebase Firestore测试中的TypeError: module.exports.v1 is not a constructor错误

我之前也碰到过一模一样的问题!这个错误本质上是firebase-admin和它依赖的@google-cloud/firestore版本不兼容导致的——当你执行npm install时,npm可能自动拉取了不符合兼容要求的最新版@google-cloud/firestore,但你的firebase-admin版本还没适配这个新的API结构。下面是几个亲测有效的解决方案:

方案1:锁定@google-cloud/firestore的兼容版本

  1. 找到你之前能正常运行的package-lock.json文件,搜索@google-cloud/firestore,记下它的具体版本号(比如6.15.0)。
  2. 在你的package.json中手动添加这个依赖的版本锁定:
    "dependencies": {
      // 其他依赖...
      "@google-cloud/firestore": "6.15.0"
    }
    
  3. 删除node_modules和package-lock.json,然后重新执行:
    npm install
    

方案2:匹配firebase-admin与@google-cloud/firestore的版本

不同版本的firebase-admin对@google-cloud/firestore有明确的兼容要求,比如:

  • firebase-admin v11.x 对应 @google-cloud/firestore v6.x
  • firebase-admin v12.x 对应 @google-cloud/firestore v7.x

你可以根据当前firebase-admin的版本,调整@google-cloud/firestore到对应大版本,或者反过来升级/降级firebase-admin到匹配的版本。

方案3:清理npm缓存并重新安装

有时候npm缓存会导致版本拉取混乱,试试彻底清理后重装:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

(注意:执行前确保package.json里的依赖版本是你确认兼容的)

方案4:检查测试代码的导入逻辑

如果你的测试代码直接引用了@google-cloud/firestore的v1构造函数,新版本的包可能已经改变了导出方式。比如旧代码可能是:

const Firestore = require('@google-cloud/firestore').v1.Firestore;

而新版本可能需要改成:

const { Firestore } = require('@google-cloud/firestore');

检查并调整代码中的导入语句,适配新版本的API结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:12:00