Search code examples
javascriptnode.jsfirebasefirebase-storagefirebase-tools

Firebase emulator storage - Can't connect to firebase storage emulator with getStorage


I wonder if somebody could me debugg why I cannot connect to the firebase emulator or share some knowledge on how to connect pls.

Goal: Connect to the Firebase storage emulator.

Context: I tryed following mostly the official documentation with no success

My code:

const { getStorage, connectStorageEmulator } = require("firebase/storage");
const admin = require("firebase-admin");

const app = admin.initializeApp();

const storage = getStorage(app); // Error: app.container is undefined

connectStorageEmulator(storage, "127.0.0.1", 9199);

Error:

/home/guest/Documents/repos/backend-v1/functions/node_modules/@firebase/app/dist/index.cjs.js:276
        .getProvider('heartbeat')
         ^

TypeError: Cannot read properties of undefined (reading 'getProvider')
    at Object._getProvider (/home/guest/Documents/repos/backend-v1/functions/node_modules/@firebase/app/dist/index.cjs.js:276:10)
    at getAuth (/home/guest/Documents/repos/backend-v1/functions/node_modules/@firebase/auth/dist/node/totp-a189ea13.js:8200:24)
    at Object.<anonymous> (/home/guest/Documents/repos/backend-v1/functions/test/extractor.spec.js:13:14)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Versions:

Linux
Node: v20.5.1

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

I expected to connect to the emulator storage, I tryed following mostly the official but an error occured.


Solution

  • The firebase/storage module is for frontend web applications. It's not for nodejs programs. Also, you can't mix it with the firebase-admin module - they are not compatible with each other at all.

    The instructions you linked are for web applications, but it appears that you are not writing a web app at all, and are instead writing a nodejs program or some other backend.

    If you are trying to write a nodejs program that runs backend code, you should remove the firebase dependency entirely and just use firebase-admin. The instructions for connecting the Firebase Admin SDK to the emulator is covered in its documentation and is completely different than what you're doing now, since it involves setting environment variables for your node process.

    If you are actually trying to write a web application, then you shouldn't use firebase-admin at all. That's only meant for nodejs programs.