部署 Firebase 函数时出现问题 - 无法读取未定义的属性“firebase-admin”

问题描述 投票:0回答:1

我在部署 Firebase 功能时遇到问题,如果您能帮助解决该问题,我将不胜感激。部署本身看起来不错,但我收到以下错误:

无法读取未定义 npm 的属性“firebase-admin”,错误!一套完整的 这次运行的日志可以在以下位置找到:

依赖关系:

  • “firebase-admin”:“^11.8.0”
  • “firebase-functions”:“^4.3.1”

当地环境:

  • 节点版本: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 函数:准备 src/app/_cloud-functions 目录 正在上传...我的功能:已打包 D:\Development\xxx\src pp_cloud-functions (42.41 KB) 用于上传

  • 功能:src/app/_cloud-functions 文件夹上传成功 i 功能:更新 Node.js 14(第一代)功能 listUsers(us-central1)...构建失败:npm ERR!无法读取属性 'firebase-admin' 未定义

npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!
/www-data-home/.npm/_logs/2024-01-15T16_37_23_763Z-debug.log;错误 编号:beaf8772

函数部署存在以下函数错误: listUsers(us-central1) i 功能:清理构建文件...

错误:部署功能时出错

{“protoPayload”:{ "@type": "type.googleapis.com/google.cloud.audit.AuditLog", “地位”: { “代码”:3, “message”:“构建失败:npm ERR!无法读取未定义的属性“firebase-admin” npm 错误!本次运行的完整日志 可以在以下位置找到: npm 错误!
/www-data-home/.npm/_logs/2024-01-15T16_52_09_038Z-debug.log;错误 编号:beaf8772” }, “身份验证信息”:{}, "serviceName": "cloudfunctions.googleapis.com", "methodName": "google.cloud.functions.v1.CloudFunctionsService.CreateFunction", “资源名称”:“项目/xyz/位置/us-central1/functions/listUsers”
}, "insertId": "-il0dwwcmlo", "资源": { “类型”:“云函数”, “标签”: { “project_id”:“xyz”, “地区”:“us-central1”, "function_name": "列表用户" } }, “时间戳”: “2024-01-15T16:52:18.282342Z”, “严重性”: “错误”, “日志名称”: “projects/xyz/logs/cloudaudit.googleapis.com%2Factivity”,
“手术”: { “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
}

如有任何帮助,我们将不胜感激。

javascript firebase npm google-cloud-functions firebase-admin
1个回答
0
投票

我通过添加解决了这个问题 "type": "module", 到package.json并将文件名index.js更改为index.cjs

© www.soinside.com 2019 - 2024. All rights reserved.