firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

Missing Exports

Open ABuffSeagull opened this issue 3 years ago • 19 comments

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

ABuffSeagull avatar Apr 26 '22 15:04 ABuffSeagull

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

google-oss-bot avatar Apr 26 '22 15:04 google-oss-bot

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'

enchorb avatar Jun 15 '22 12:06 enchorb

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

JeffyLo94 avatar Sep 28 '22 18:09 JeffyLo94

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;

koistya avatar Jan 20 '23 00:01 koistya

FYI - The Firebase App (not admin) also has a similar issue open: https://github.com/firebase/firebase-js-sdk/issues/4551

minyoon avatar Jan 21 '23 09:01 minyoon

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 avatar Feb 14 '23 15:02 tuotau

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

tuotau avatar Feb 16 '23 12:02 tuotau

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

mapokapo avatar Feb 27 '23 14:02 mapokapo

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

andreidragu avatar Apr 25 '23 11:04 andreidragu

Any update on this?

jonatandorozco avatar May 27 '23 03:05 jonatandorozco

Any update?

jketcham avatar Jul 19 '23 03:07 jketcham

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

spock123 avatar Aug 19 '23 06:08 spock123

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

koistya avatar Aug 19 '23 13:08 koistya

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

daviddomkar avatar Oct 01 '23 21:10 daviddomkar

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

image

utkarsh1097 avatar Nov 09 '23 15:11 utkarsh1097