title: node js module exports for firebase initialization function
body: <p>I was trying to use firebase-admin in my node js project. below is code of my main page:</p>
  
  <pre><code>const functions = require('firebase-functions');
  const express = require('express');
  const  ejs = require('ejs');
  var firebase = require('firebase-admin');
  const app = express();
  
  // const firebaseApp = firebase.initializeApp(
  //     functions.config().firebase
  // );
  app.set('view engine', 'ejs');
  app.set('views', './views');
  
  app.use('/', require('./routes/index'));
  app.use('/getPosts', require('./routes/posts'));
  app.use('/tests', require('./routes/tests'));
  exports.app = functions.https.onRequest(app);
  exports.firebaseApp = function(){
      return firebase.initializeApp(
          functions.config().firebase
      );
  }
  </code></pre>
  
  <p>The code of the file - <strong>./routes/tests</strong> is given below:</p>
  
  <pre><code>const functions = require('firebase-functions');
  var express = require('express');
  var router = express.Router();
  var firebase = require('firebase-admin');
  //
  // const firebaseApp = firebase.initializeApp(
  //     functions.config().firebase
  // );
  const firebaseApp = require('../index.js').firebaseApp;
  function getPosts(){
      const ref = firebaseApp.database().ref('users');
      return ref.once('value').then(snap =&gt; snap.val());
  }
  
  router.get('/', (req, res, next) =&gt; {
      getPosts().then(posts =&gt; {
          res.json(posts);
      });
  
  
  });
  module.exports = router;
  </code></pre>
  
  <p>The problem is, when I visit <code>http://localhost:5000/getPosts</code>, it says <strong>TypeError: Cannot read property 'database' of undefined</strong>. What is the preoblem here?
  Adding </p>
  
  <pre><code>firebase.initializeApp(
          functions.config().firebase
      );
  </code></pre>
  
  <p>in every layout page, is not a good idea because it'll initialize firebase multiple times.
  How do I solve this issue ?</p>