Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase admin Can't read property 'cert' of undefined

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.

like image 993
GrepThis Avatar asked Jun 07 '20 19:06

GrepThis


1 Answers

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';
like image 55
riversun Avatar answered Nov 01 '22 18:11

riversun