Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Exports #1666

Closed
ABuffSeagull opened this issue Apr 26, 2022 · 20 comments
Closed

Missing Exports #1666

ABuffSeagull opened this issue Apr 26, 2022 · 20 comments
Assignees

Comments

@ABuffSeagull
Copy link

[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.

@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@enchorb
Copy link

enchorb commented Jun 15, 2022

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'

@JeffyLo94
Copy link

Any updates on this issue? On Typescript, unable properly catch FirebaseAuthError type exceptions.

@koistya
Copy link

koistya commented Jan 20, 2023

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;

@minyoon
Copy link

minyoon commented Jan 21, 2023

FYI - The Firebase App (not admin) also has a similar issue open: firebase/firebase-js-sdk#4551

@tuotau
Copy link

tuotau commented Feb 14, 2023

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.

@tuotau
Copy link

tuotau commented Feb 16, 2023

@lahirumaramba Any updates on whether something could be done for this?

@mapokapo
Copy link

  • FirebaseError (from firebase-admin) and AuthError (from @firebase/auth) are Typescript interfaces, not classes, so they cannot be used in instanceof.
  • FirebaseError (from @firebase/util) is a class, but for some reason it doesn't catch the elusive FirebaseAuthError in instanceof checks.

The only solution I have found so far is to:

  1. Check if the error is an instanceof Error
  2. Create a new variable firebaseError and set it equal to the error, then cast it to FirebaseError
  3. 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.

@andreidragu
Copy link

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
  }
}

@jonatandorozco
Copy link

Any update on this?

@jketcham
Copy link

Any update?

@spock123
Copy link

Guys this is kinda crazy this isn't addressed... error handling and parsing is a very vital part of development.

@koistya
Copy link

koistya commented Aug 19, 2023

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";

@daviddomkar
Copy link

This needs to be exported asap. Error handling with admin SDK is a nightmare.

@utkarsh1097
Copy link

#1666 (comment)

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

image

@FelixKahle
Copy link

Got the same issue here. Cannot import anything from ./utils when using ESM directory. This is very inconvenient, as we now have to do some ugly type checking to be sure that we indeed have a Firebase Error. Providing exports in the future would be highly appreciated.

@Deliana90
Copy link

🔥RIP firebase 🔥

@lahirumaramba
Copy link
Member

Thanks for your patience folks! I understand your frustration. Until we get better error handling implemented in this SDK, I will merge #2151 for now. That should solve most of the issues you have mentioned above.

@lahirumaramba
Copy link
Member

lahirumaramba commented May 8, 2024

With the new changes in #2151 and #2549, the following works.

import { initializeApp } from 'firebase-admin/app';
import { getAuth, FirebaseAuthError } from 'firebase-admin/auth';

async function main() {
  const app = initializeApp();
  const user = await getAuth().getUserByEmail('[email protected]');
}

main().catch((e) => {
  if (e instanceof FirebaseAuthError) {
    console.log(e.code);
  }
});

We will include the changes in the upcoming release. Thanks!

@lahirumaramba
Copy link
Member

The above changes are now released in v12.1.1. I will close this issue for now. Please open a new issue if you run into any problems. Thanks folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests