部署Firebase Functions报错:Cannot read property 'firebase-admin' of undefined
部署Firebase Functions时出现
Cannot read property 'firebase-admin' of undefined错误 问题概述
部署Firebase Functions流程看似正常,但触发构建错误:Cannot read property 'firebase-admin' of undefined
依赖项
- "firebase-admin": "^11.8.0"
- "firebase-functions": "^4.3.1"
本地环境
- Node版本:v20
函数代码
import * as admin from "firebase-admin"; import * as functions from 'firebase-functions'; const serviceAccount = require('./mooland-999ad-e0753014493a.json'); const app: admin.app.App = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }); exports.listUsers = functions.https.onRequest(async (req, res) => { try { const listUsersResult = await app.auth().listUsers(); const users = listUsersResult.users.map((userRecord) => ({ id: userRecord.uid, email: userRecord.email, name: userRecord.displayName, // Add more user information as needed })); res.json(users); } catch (error) { console.error('Error listing users:', error); res.status(500).send('Internal Server Error'); } });
错误日志
i functions: preparing src/app/_cloud-functions directory for uploading... i functions: packaged D:\Development\xxx\src\app_cloud-functions (42.41 KB) for uploading - functions: src/app/_cloud-functions folder uploaded successfully i functions: updating Node.js 14 (1st Gen) function listUsers(us-central1)... Build failed: npm ERR! Cannot read property 'firebase-admin' of undefined npm ERR! A complete log of this run can be found in: npm ERR! /www-data-home/.npm/_logs/2024-01-15T16_37_23_763Z-debug.log; Error ID: beaf8772 Functions deploy had errors with the following functions: listUsers(us-central1) i functions: cleaning up build files... Error: There was an error deploying functions { "protoPayload": { "@type": "type.googleapis.com/google.cloud.audit.AuditLog", "status": { "code": 3, "message": "Build failed: npm ERR! Cannot read property 'firebase-admin' of undefined\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR! /www-data-home/.npm/_logs/2024-01-15T16_52_09_038Z-debug.log; Error ID: beaf8772" }, "authenticationInfo": {}, "serviceName": "cloudfunctions.googleapis.com", "methodName": "google.cloud.functions.v1.CloudFunctionsService.CreateFunction", "resourceName": "projects/xyz/locations/us-central1/functions/listUsers" }, "insertId": "-il0dwwcmlo", "resource": { "type": "cloud_function", "labels": { "project_id": "xyz", "region": "us-central1", "function_name": "listUsers" } }, "timestamp": "2024-01-15T16:52:18.282342Z", "severity": "ERROR", "logName": "projects/xyz/logs/cloudaudit.googleapis.com%2Factivity", "operation": { "id":
package.json
{ "name": "functions", "description": "Cloud Functions for Firebase", "scripts": { "build": "tsc", "serve": "firebase emulators:start --only functions", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "engines": { "node": "16" }, "main": "index.js", "dependencies": { "firebase-admin": "^11.8.0", "firebase-functions": "^4.3.1" }, "devDependencies": { "firebase-functions-test": "^3.1.0" }, "private": true }
解决方案
1. 对齐Node.js版本
本地Node版本为v20,但package.json的engines字段指定v16,部署时Cloud Functions会使用指定版本,易引发依赖安装冲突。修改engines字段为与本地一致的版本:
"engines": { "node": "20" }
2. 清理并重新安装依赖
- 执行命令清理npm缓存:
npm cache clean --force - 删除本地的
node_modules文件夹和package-lock.json文件 - 重新安装依赖:
npm install
3. 简化Admin SDK初始化
在Cloud Functions环境中,无需手动加载serviceAccount文件,Firebase会自动初始化Admin SDK。修改初始化代码:
import * as admin from "firebase-admin"; import * as functions from 'firebase-functions'; // 无需手动传入serviceAccount,自动使用默认凭据 admin.initializeApp(); exports.listUsers = functions.https.onRequest(async (req, res) => { // 原有业务逻辑保持不变 });
4. 确认部署目录正确性
确保执行firebase deploy --only functions命令时,当前目录是包含package.json的functions目录,避免上传错误的目录结构导致依赖解析失败。
内容的提问来源于stack exchange,提问作者MuhanadY
相关产品推荐
相关产品推荐

