0
votes

I added Firebase Admin SDK to a react project (I used create-react-app if that makes a difference). I'm just testing it out so I just initialize Firebase in index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';

var admin = require("firebase-admin");

var serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<APP-NAME>.firebaseio.com/"
});

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Everything else in the project is essentially untouched. I go to run npm run build so I can deploy the project to Firebase hosting and get the following failed to compile error

Module not found: Error: Cannot resolve module 'request' in /home/ubuntu/workspace/shelf/node_modules/firebase-admin/lib/database

Any ideas what is the issue here or what I should do to solve it? Thanks!

1
The Firebase Admin SDK is meant for server-side applications, not client-side applications. You should use the standard Firebase client SDK (firebase not firebase-admin) in your React app. - jwngr
@jacobawenger I have a server.js file that handles a POST request at a given url. Wouldn't I need the admin sdk for that? - maxwellgover
The code sample you provided is client-side code, right? It uses "react-dom" after all. Is there a reason why you cannot use the client-side Firebase library and need to use the Admin SDK? - jwngr
To authenticate sever with firebase so I can store data in the database. - maxwellgover

1 Answers

1
votes

The Firebase Admin SDK is intended to be un on NodeJS, hence why it is trying to require request.

Please use the Firebase Client package for your React App.