-
-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Title:* App crashes on npm run dev
if Firebase API key is missing – Firebase should only initialize when needed
Description:
When cloning the project and running npm run dev
without a .env
file or Firebase API key, the app immediately crashes due to an unconditional call to getAuth()
from Firebase. This occurs even in parts of the app that do not require Firebase, which should not happen.
Expected Behavior:
Firebase should only be initialized when explicitly required. The app should not crash during local development just because the .env
is missing or Firebase is not needed for the current route/component.
Steps to Reproduce:
-
Clone the project.
-
Do not create a
.env
file or add Firebase credentials. -
Run:
npm install npm run dev
-
Observe the crash caused by
getAuth()
being invoked without a valid Firebase config.
Environment:
- Next.js: 15.1.4
- Firebase SDK: 11.3.1
- Node.js: 23.10.0
Error Cause:
Calling getAuth()
or initializing Firebase without checking for required config values like apiKey
leads to a crash.
Suggested Fix:
Ensure Firebase is only initialized when its config is present. For example:
let auth;
if (process.env.NEXT_PUBLIC_FIREBASE_API_KEY) {
const app = initializeApp(firebaseConfig);
auth = getAuth(app);
}
Or lazy-load Firebase only where it’s actually needed, rather than globally or unconditionally.