javascriptnode.jsfirebasejavascript-import

no "export" main defined in "node_modules\firebase\package.json"


I want to use firebase Realtime database with express but one line is causing an error: no "export" main defined in "node_modules\firebase\package.json"

config aka firebaseForServer.js file:

const firebase = require("firebase");
const { getDatabase } = require("firebase/database");
const { initializeApp } = require("firebase/app");

const firebaseConfig = {};

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
module.export = db;

server.js file:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const router = express.Router();
const db = require("./firebaseForServer");

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

I tried to remove lines one by one to see which line is causing this error and I found that line const db = require("./firebaseForServer"); is causing the error. Please tell me what this error means and how I can solve it/


Solution

  • We learn that there is no default export in the firebase package entry point (node_modules/firebase/app/dist/app/index.d.ts) from the runtime error message.

    "export" main defined in "node_modules\firebase\package.json"
    

    We can correct this by removing the declared import below:

    const firebase = require("firebase");
    

    The declared import is not used anyway in the firebaseForServer module.

    Also, correct the declared exports in firebaseForServer. In CommonJS module specification, the declared exports must be specified by assigning it to module.exports object.

    const { initializeApp} = require("firebase/app");
    const { getDatabase } = require("firebase/database");
    const { getFirestore } = require("firebase/firestore/lite");
    
    
    const firebaseConfig = {};
    
    const db = getDatabase(app);
    
    module.exports = db;