问题
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: Firebase-Admin@5.8.0 @firebase/app@0.1.1 @firebase/database@0.1.3
回答1:
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.
来源:https://stackoverflow.com/questions/48266195/typeerror-rtdb-initstandalone-is-not-a-function