I'm receiving a persistent error when deploying my Firebase Cloud Functions: "TypeError: Cannot read properties of undefined (reading 'document')". This happens while I define a Firestore trigger using the v2 API, specifically with the call to functions.firestore.v2.document(...)
.
Below is the relevant code snippet:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const axios = require("axios");
const { FieldValue } = require('firebase-admin/firestore');
admin.initializeApp();
const db = admin.firestore();
exports.rahh = functions.firestore.v2.document("user/{userId}").onCreate(async (event) => {
// ... (My function logic here - fetching data from Google People API) ...
});
The error message:
TypeError: Cannot read properties of undefined (reading 'document')
at Object. (C:\path\to\my\functions\index.js:9:39)
... (Rest of the stack trace) ...
Despite initializing Firebase Admin with admin.initializeApp();
and requiring the correct modules, it appears that the firestore
namespace on the functions
object is undefined. I've taken the following troubleshooting steps to isolate the issue:
- Resetting the emulator with
firebase emulators:stop
, firebase emulators:clear
, and firebase emulators:start --only functions
- Switching to Node 18 using nvm
- Running on the emulator, which worked perfectly
- Removing
package-lock.json
and node_modules
then reinstalling with npm install
- Updating the global Firebase CLI with
npm install -g firebase-tools
- Testing with a simplified index.js containing a basic HTTPS function
- Confirming the
main
field in package.json is set to "index.js"
- Checking for typos in imports and function calls
- Editing in a basic text editor to rule out IDE-specific issues
Here is my package.json file:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": { /* ... */ },
"engines": { "node": "18" },
"main": "index.js",
"dependencies": {
"axios": "^1.7.9",
"firebase": "^11.2.0", // Firebase client SDK (might not be needed in functions)
"firebase-admin": "^13.0.2",
"firebase-functions": "^6.3.1"
},
"devDependencies": { /* ... */ },
"private": true
}
I'm also using Firebase CLI version 13.29.3. A new project works without issues, which suggests there might be a configuration mismatch in my current setup. What could be causing this "undefined" error despite all the correct imports and initializations?
I'm aware of a couple of possibilities:
- Using v2 Firestore triggers in a 1st generation environment, where the v2 namespace might not be enabled.
- A project configuration issue that prevents exposing the Cloud Functions 2nd gen features.
Any guidance on troubleshooting or resolving this error would be greatly appreciated.