I have a Typescript Nx + Next.js App running with Firebase (and Firebase Admin). Within this codebase I have a firebase admin util defined as follows -
// File ./utils/FirebaseAdmin.ts
// import checkConfig from './checkConfig';
import firebaseAdmin, { cert } from 'firebase-admin/app';
import 'firebase-admin/auth';
import 'firebase-admin/firestore';
import { getAuth } from 'firebase-admin/auth';
import { getFirestore } from 'firebase-admin/firestore';
let FirebaseAdmin;
const credentials = {
projectId: process.env.NEXT_PUBLIC_FIREADMIN_PROJECT_ID,
privateKey: process.env.NEXT_PUBLIC_FIREADMIN_PRIVATE_KEY,
clientEmail: process.env.NEXT_PUBLIC_FIREADMIN_CLIENT_EMAIL,
};
const options = {
credential: cert(credentials),
};
if (firebaseAdmin.getApps().length === 0) {
// checkConfig(credentials, true);
FirebaseAdmin = firebaseAdmin.initializeApp(options);
}
// export const FirebaseAdmin = getApp();
export const FirebaseAdminAuth = getAuth(FirebaseAdmin);
export const FirestoreAdmin = getFirestore(FirebaseAdmin);
export default FirebaseAdmin;
But when using this either within the page getServerSideProps
function or when using this in the nextjs API as follows -
// ./pages/api/projects.ts
import FirebaseAdmin from '../utils/FirebaseAdmin'; // <- at the start of the file
....
....
const SomeMethod = (handler) => {
....
const adminAuth = getAuth(FirebaseAdmin);
....
}
export default SomeMethod;
or when even when referencing FirestoreAdmin
from the util I get the below error -
ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
at Module.default (webpack-internal:///./utils/FirebaseAdmin.ts:6:42)
After trying various approaches, and hours of googling around, I am still not close to a solution, this seems like some sort of execution order issue. But I could be wrong. Any clue as to what could be causing this issue or known solution would be very helpful.
Some Package Versions used -
"next": "12.0.0",
"firebase": "^9.5.0",
"firebase-admin": "^10.0.0",