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
In your
irebaseForServer.js
, change the last line from,to
You can either use
module.exports
orexports
. But there is nomodule.export
. Hence you error.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.We can correct this by removing the declared import below:
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.