Description
After making several changes to our codebase recently, we discovered that we are unable to access firebase-admin
sub-classes in various namespaces with the emulator. This may be an issue with firebase-admin
itself, but the errors we are getting (see below) only appear when emulating our functions, and not on live code.
Others experiencing similar issues with firebase-admin
posted about it on an issue (transferred to discussion) on that project's repo. See this comment by pascalbe-dev. Granted, most comments in that thread seem to be related to esm
imports, and here we're using commonjs
. I'm not sure what to make of it.
Environment info
firebase-tools: 11.21.0
Platform: Ubuntu (GitHub Codespace)
Test case
functions/index.js
const https = require('firebase-functions/v1/https');
const admin = require('firebase-admin');
admin.initializeApp();
module.exports.callableTest = https.onCall((data, context) => {
const timestamp = admin.firestore.Timestamp.now();
console.log(`timestamp:`, timestamp);
});
index.js
// You will need to initialize Firebase JS SDK on local
firebase.functions().useEmulator('localhost', 8080); // Or your environment default
firebase.functions().httpsCallable('callableTest')();
// "Unhandled error TypeError: Cannot read properties of undefined (reading 'now')"
functions/package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.1.1"
},
"private": true,
"type": "commonjs",
"engines": {
"node": "16"
}
}
Steps to reproduce
Provided you have correctly initialized Firebase on the client, you can simply run firebase emulators:start --only functions
and run /index.js
to call the test function. (Truthfully, I opened a page in our app and simply ran those two lines in the Chrome console. I don't believe the way in which the http function is called is relevant to the issue.)
Expected behavior
Firebase Emulator UI would have logged the current timestamp (timestamp object { seconds: number, nanoseconds: number }
).
Actual behavior
We get an error that looks like:
Unhandled error TypeError: Cannot read properties of undefined (reading 'now')
at /workspaces/test_repo/functions/index.js:7:47
at newHandler (/home/codespace/.nvm/versions/node/v16.19.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:317:16)
at fixedLen (/workspaces/test_repo/functions/node_modules/firebase-functions/lib/v1/providers/https.js:74:41)
at /workspaces/test_repo/functions/node_modules/firebase-functions/lib/common/providers/https.js:411:32
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async runFunction (/home/codespace/.nvm/versions/node/v16.19.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:504:9)
at async runHTTPS (/home/codespace/.nvm/versions/node/v16.19.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:529:5)
at async /home/codespace/.nvm/versions/node/v16.19.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21
We first noticed this with admin.firestore.Timestamp
, but then also with admin.database.ServerValue
. In both cases, admin.firestore
and admin.database
are both empty objects, rendering Timestamp
and ServerValue
undefined.
If, however, you boot up a local node session, you can get the following:
@tannerstern $ node
Welcome to Node.js v16.19.0.
Type ".help" for more information.
> const admin = require('./functions/node_modules/firebase-admin')
undefined
> admin.firestore.Timestamp.now()
Timestamp { _seconds: 1674591147, _nanoseconds: 187000000 }
>