MuhanadY
MuhanadY

Reputation: 730

Issue deploying Firebase functions - Cannot read property 'firebase-admin' of undefined

I'm encountering an issue while deploying Firebase functions, and I would appreciate any assistance in resolving it. The deployment itself seems fine, but I'm getting the following error:

Cannot read property 'firebase-admin' of undefined npm ERR! A complete log of this run can be found in:

Dependencies:

Local Environment:

Function Code:

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');
  }
});

Error log:

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
}

Any help is appreciated.

Upvotes: 1

Views: 229

Answers (1)

MuhanadY
MuhanadY

Reputation: 730

I solved the issue by adding "type": "module", to package.json and changing the file name index.js to index.cjs

Upvotes: -1

Related Questions