skip to Main Content

I want to use firebase Realtime database with express but one line is causing an error: no "export" main defined in "node_modulesfirebasepackage.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/

2

Answers


  1. In your irebaseForServer.js, change the last line from,

    module.export = db;
    

    to

    module.exports = db;
    

    You can either use module.exports or exports. But there is no module.export. Hence you error.

    Login or Signup to reply.
  2. 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_modulesfirebasepackage.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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search