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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With