1

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:

  • "firebase-admin": "^11.8.0"
  • "firebase-functions": "^4.3.1"

Local Environment:

  • Node version: v20

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.

4
  • 1
    You've been provided a reference to an error log, which you're not showing here. Please update the question to include the relevant parts of that log. Ideally you should be able to narrow this down to the specific line of code that causes the problem by first eliminating everything, then adding lines back slowly to find out which one is the problem. Commented Jan 15, 2024 at 16:30
  • are you sure the dependency is download? It can only be listed on package.json but not really installed. You can try to remove node_modules folder and reinstalling all dependencies
    – luicfrr
    Commented Jan 15, 2024 at 16:34
  • @NoNam4 the exception is not local it's on the server side. everything works ok on locally. even emulators do work
    – MuhanadY
    Commented Jan 15, 2024 at 16:42
  • @DougStevenson thank you for your advice. I already did reduce the lines, but the point is from the first line! not able to find firebase-admin!
    – MuhanadY
    Commented Jan 15, 2024 at 16:56

1 Answer 1

-1

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

1
  • This is a "workaround" and not a solution. Ultimately it looks to me like you are trying to use an old code sample of the "namespaced API" for the Admin SDK rather than the newer "modular API". I suspect if you followed the newer API syntax that a lot of these errors will go away. Otherwise you will be bending over backwards to maintain use of the older syntax which is already deprecated. Commented Jan 16, 2024 at 4:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.