3
votes

I'm pretty new to both Node and Firebase, but can't seem to find this error online.

I get the following error when I try to load Firebase Database through Firebase Admin:

TypeError: rtdb.initStandalone is not a function DatabaseService../node_modules/firebase-admin/lib/database/database.js.DatabaseService.getDatabase node_modules/firebase-admin/lib/database/database.js:62

    var db = this.INTERNAL.databases[dbUrl];
    if (typeof db === 'undefined') {
        var rtdb = require('@firebase/database');
        db = rtdb.initStandalone(this.appInternal, dbUrl).instance;
        this.INTERNAL.databases[dbUrl] = db;
    }
    return db;

Node Module Versions: [email protected] @firebase/[email protected] @firebase/[email protected]

1
It looks like you're trying to use both the Firebase Admin SDK and the Firebase web client SDK in the same program. That's probably not what you want. If you're writing server code, just use the admin SDK and don't bother with the client SDK. - Doug Stevenson
Hi Doug, thanks for the response. According to package.json, firebase-admin holds firebase-database as a dependancy - I don't think I've installed firebase web client, but can't be sure... I did try adding firebase as a core package and update to try to fix the above problem originally (but obviously to no avail). - Julian Quirke
I've tried reimporting all package for the project from scratch including firebase-admin, react, react-dom, react-scripts (through create-react-app), and react-router, but the same error is occurring. - Julian Quirke
Don't use the client SDKs directly in server side code, even if you see them as part of your node modules. Use the admin SDK only. firebase.google.com/docs/admin/setup - Doug Stevenson
I should have read the docs better - looks like i was using admin SDK when I should have been using web client SDK the entire time. - Julian Quirke

1 Answers

11
votes

In case anyone has the same issue I did:

@firebase/database/package.json:

"main": "dist/cjs/index.node.js",
"browser": "dist/cjs/index.js",
"module": "dist/esm/index.js",

initStandalone() is in dist/cjs/index.node.js but not dist/esm/index.js because it's only used serverside, not in the browser.

If you use webpack to bundle your code, even with target: 'node', it will prefer "module" over "main" and use the file intended for the browser.

The solution for me was to set resolve.mainFields to ["main", "module"] in webpack config.

PS. I'm using webpack to prepare my code for upload to AWS Lambda. If I don't use it I have to upload the entire node_modules folder which is huge and often causes timeouts on upload. I only need 10% of the code that's in there. If anyone knows of a better solution I'm keen to hear it.