Firebase admin Can't read property 'cert' of undefined
Asked Answered
A

3

8

I writing a hapi backend api with firebase admin. I can't find a fix for this error.

TypeError: Cannot read property 'cert' of undefined at ModuleJob.run (internal/modules/esm/module_job.js:109:37) at async Loader.import (internal/modules/esm/loader.js:133:24)

Code:

import * as admin from 'firebase-admin';
import serviceAccount from './resources/serviceAccount.js'
let storageBucket = process.env.NODE_ENV === 'production' ? '' : 'gs://myapp-dev.appspot.com'

let app = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: process.env.NODE_ENV === 'production' ? "https://myapp.firebaseio.com" : "https://jointcreative1-dev.firebaseio.com"
});

Package json:

 {
  "name": "backend",
  "version": "1.0.0",
  "description": "Hapi/joi backend server",
  "type": "module",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "XXX",
  "license": "MIT",
  "dependencies": {
    "@hapi/hapi": "^19.1.1",
    "firebase": "^7.14.6",
    "firebase-admin": "^8.12.1",
    "firebase-tools": "^8.4.1",
    "google-auth-library": "^0.12.0",
    "hapi-auth-jwt2": "^10.1.0"
  }
}

I was using it in sapper and everything worked fine but moving into hapi broke it for some reason.

This was addressed a few years ago but multiple major versions have been released since then.

Ambrotype answered 7/6, 2020 at 19:22 Comment(0)
J
18

As written in package.json as "type":" module"

It looks like you are using ES Modules, so try the following:

import admin from 'firebase-admin';
Jerrine answered 5/11, 2020 at 14:57 Comment(2)
This fixed my problem. What is beyond me is why it worked with "*as admin" on dev but not on production being every config equal....Plosion
To highlight the difference ... don't use import * asPaleoecology
P
0

Did you try this one?

var serviceAccount = require('./resources/serviceAccount.js');
let app = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  ...
});
Pentane answered 7/6, 2020 at 21:17 Comment(0)
A
0

Change:

import * as admin from 'firebase-admin'

To:

import admin from 'firebase-admin';
Atmospherics answered 19/2 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.