Home > Net >  Express with Firebase require() throws error
Express with Firebase require() throws error

Time:10-13

I have an Express application that I am trying to connect to Firebase.

My console is currently throwing this error:

C:\Users\Tyler\Desktop\Dev\food-bank\app.cjs:19
const firebase = initializeApp(firebaseConfig);
                 ^

TypeError: initializeApp is not a function
    at Object.<anonymous> (C:\Users\Tyler\Desktop\Dev\food-bank\app.cjs:19:18)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
[nodemon] app crashed - waiting for file changes before starting...

The offending code is here:

let initializeApp = require('./node_modules/@firebase/app');

I don't want to change my package.json to use import statements. The Firebase docs have import module statements but because I'm using Express I want to use the require statements.

How can I fix this?

CodePudding user response:

I think you're simply missing the destructuring brackets in the require statement:

let { initializeApp } = require('./node_modules/@firebase/app');

Also, normally you should simply be able to require('@firebase/app') and not specify the relative path to packages inside node_modules:

    let { initializeApp } = require('@firebase/app');
  • Related