Home > Mobile >  initialise firebase in nodejs expressjs
initialise firebase in nodejs expressjs

Time:02-14

i want to initialise a firebase (not firebase-admin) instance in Node.

import { initializeApp } from 'firebase/app';

const firebaseConfig = {
  //...
};

const app = initializeApp(firebaseConfig);

this won't work cause node works with commonjs exports and you can't require from firebase cause there is no exports..

CodePudding user response:

Try this.

    const firebase = require('firebase')

    const firebaseConfig = {
        //....
    }

    const app = firebase.initializeApp(firebaseConfig);

CodePudding user response:

https://www.npmjs.com/package/firebase says:

This SDK is intended for end-user client access from environments such as the Web, mobile Web (e.g. React Native, Ionic), Node.js desktop (e.g. Electron), or IoT devices running Node.js. If you are instead interested in using a Node.js SDK which grants you admin access from a privileged environment (like a server), you should use the Firebase Admin Node.js SDK.

So use firebase-admin instead if you are planning to use firebase with the backend.

Install with npm install firebase-admin --save

To initialize define a variable export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json" and

const { initializeApp } = require('firebase-admin/app');
initializeApp()
  • Related