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

部署Firebase Functions时出现npm读取'firebase'属性未定义错误

解决Firebase Functions部署报错:npm ERR! Cannot read property 'firebase' of undefined

问题根源

这个报错大概率是依赖版本冲突或配置不当导致的,结合你的场景,核心问题集中在这几点:

  • firebase-tools被错误放在dependencies中,它属于工具类依赖,不该出现在生产依赖里
  • TypeScript 5.x和旧版ESLint插件(v3.x)版本不兼容,引发解析错误
  • 不必要的firebase客户端库可能和firebase-admin产生依赖冲突

修复步骤

1. 调整package.json依赖配置

修改你的package.json,做以下关键改动:

  • 将firebase-tools移到devDependencies(或直接全局安装npm install -g firebase-tools更推荐)
  • 移除dependencies里的firebase客户端库(Firebase Functions服务端环境不需要,firebase-admin已包含所有所需服务端能力)
  • 升级ESLint相关依赖到兼容TypeScript 5.x的版本
  • 把Node版本从14升级到18(Node 14已被Firebase废弃,18是当前推荐的稳定版本)

修改后的package.json示例:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "buildForDeploy": "npm run lint && tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "18"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^12.0.0",
    "firebase-functions": "^4.8.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.45.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.28.0",
    "firebase-functions-test": "^3.1.0",
    "firebase-tools": "^12.5.0",
    "typescript": "^5.1.6"
  },
  "private": true
}

2. 彻底清理并重装依赖

执行以下命令清除旧依赖残留,重新安装匹配的版本:

rm -rf node_modules package-lock.json
npm install

3. 简化初始化代码

你的index.ts可以去掉重复的导入逻辑,简化为:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

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

// 你的函数代码...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 06:17:38