Search code examples
javascriptreactjstypescriptfirebasefirebase-admin

Uncaught TypeError: os.platform is not a function at credential-internal.js:38:20


I'm creating a reusable library, im importing firebase admin sdk in my lib and calling my exported function inside my react app.

enter image description here

When ever i do console.log(admin) or try to use anything imported from 'firebase-admin', Im getting weired errors.

enter image description here

Help me out. I am doing it in a correct approach. what i really wanted is to create a reusable library based on firebase and firebase-admin sdk. My app is built with NX Repo.

Let me know how to fix it or what am i doing wrong ? Thanks in advance.


Solution

  • You can't use firebase-admin directly in a React app, or any other frontend framework. The JavaScript SDK requires a nodejs backend because it's used to perform privileged operations on your project that would be insecure to expose to your frontend.

    The documentation makes this fairly clear:

    Add the Firebase Admin SDK to your server

    The Admin SDK is a set of server libraries that lets you interact with Firebase from privileged environments to perform actions like:

    The way you're supposed to the the admin SDK in a web application is by having your app invoke a backend endpoint to perform the work on the user's behalf.