firebase-admin-node
firebase-admin-node copied to clipboard
Missing Exports
[READ] Step 1: Are you in the right place?
- For issues related to the code in this repository file a Github issue.
- If the issue pertains to Cloud Firestore, read the instructions in the "Firestore issue" template.
- For general technical questions, post a question on StackOverflow with the firebase tag.
- For general Firebase discussion, use the firebase-talk google group.
- For help troubleshooting your application that does not fall under one of the above categories, reach out to the personalized Firebase support channel.
[REQUIRED] Step 2: Describe your environment
- Operating System version: Linux 5.13.0
- Firebase SDK version: 10.1.0
- Firebase Product: all, basically (auth, database, storage, etc)
- Node.js version: 16.14.0
- NPM version: 8.4.1
[REQUIRED] Step 3: Describe the problem
There are certain files that aren't listed in the exports key in the package.json. Namely everything in the utils folder, as I realized that the FirebaseAuthError is only exported by the utils/error.js file, and is nowhere in the Auth namespace. This makes it very difficult to deal with errors in TypeScript, as the usual checking with instanceof can't work without the exported class.
Not sure if the fix is to make the utils folder "public", or to just reexport the contained classes in the correct namespaces.
Also, the utils folder only exists under the CommonJS build, and not the ESM directory.
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Having the same issue, can't use instanceof for FirebaseError. Temporary hacky workaround that seems to be working for my use case is err?.name === 'FirebaseError'
Any updates on this issue? On Typescript, unable properly catch FirebaseAuthError type exceptions.
import { auth, apps } from "firebase-admin";
^^^^
SyntaxError: Named export 'apps' not found. The requested module 'firebase-admin' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'firebase-admin';
const { auth, apps } = pkg;
FYI - The Firebase App (not admin) also has a similar issue open: https://github.com/firebase/firebase-js-sdk/issues/4551
I'm having the same issue. There's currently no way to import class FirebaseAuthError to check whether the exception is an instanceof FirebaseAuthError.
For example firebase.auth().createUser(...) throws an exception that is an instanceof FirebaseAuthError and currently you can't properly check whether the exception is indeed of this type.
@lahirumaramba Any updates on whether something could be done for this?
FirebaseError(fromfirebase-admin) andAuthError(from@firebase/auth) are Typescript interfaces, not classes, so they cannot be used ininstanceof.FirebaseError(from@firebase/util) is a class, but for some reason it doesn't catch the elusiveFirebaseAuthErrorininstanceofchecks.
The only solution I have found so far is to:
- Check if the error is an
instanceofError - Create a new variable
firebaseErrorand set it equal to the error, then cast it toFirebaseError - Use firebaseError in your checks.
Example:
import { FirebaseError } from "@firebase/util"; // @firebase is included with firebase-admin
try {
// Some firebase stuff...
} catch (error) {
if (error instanceof Error) {
const firebaseError = error as FirebaseError;
console.log(firebaseError.code); // no warnings
}
}
Note that this assumes that the error variable will always extend from FirebaseError, so if you're calling other non-Firebase functions in the try block that might throw Errors, then this might break.
My approach was to use a type guard:
import type { FirebaseAuthError } from 'firebase-admin/lib/utils/error'
export const isFirebaseAuthError = (error: unknown): error is FirebaseAuthError => {
return (error as FirebaseAuthError).code.startsWith('auth/')
}
usage:
try {
// firebase...
} catch (error) {
if (isFirebaseAuthError(error)) {
console.log(error.message) // here you will have autocomplete
}
}
Any update on this?
Any update?
Guys this is kinda crazy this isn't addressed... error handling and parsing is a very vital part of development.
This syntax works fine (example), as opposed to importing from firebase-admin:
import { initializeApp } from "firebase-admin/app";
import { DecodedIdToken, getAuth } from "firebase-admin/auth";
import { Firestore, getFirestore } from "firebase-admin/firestore";
This needs to be exported asap. Error handling with admin SDK is a nightmare.
https://github.com/firebase/firebase-admin-node/issues/1666#issuecomment-1521626281
This works well for my use case, and it can also be generalized for any kind of FirebaseError. But it would be really nice if the class is exported, as discussed above.
https://firebase.google.com/docs/reference/admin/error-handling#structure-of-an-api-error