I am trying to add new Cloud Functions to my project, but I am stuck on a deployment error. Even after a clean reinstall of Node.js and Firebase tools, I cannot get past the build step. The Error:
The firebase deploy --only functions command fails during the tsc build step with the following error:
src/index.ts:8:31 - error TS2307: Cannot find module 'firebase-functions/v2/auth' or its corresponding type declarations. 8 import { onUserCreated } from "firebase-functions/v2/auth"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This error indicates that the v2 modules cannot be found, even though my package.json requires the correct version.
My Files: Here are my main configuration and code files:
package.json: { "name": "functions", "scripts": { "build": "tsc", "deploy": "firebase deploy --only functions", "lint": "eslint --ext .js,.ts ." }, "engines": { "node": "18" }, "main": "lib/index.js", "dependencies": { "firebase-admin": "^12.0.0", "firebase-functions": "^5.0.1", "@google/generative-ai": "^0.24.1" }, "devDependencies": { "typescript": "^5.0.0", "eslint": "^8.9.0", "eslint-config-google": "^0.14.0", "firebase-functions-test": "^3.1.0" }, "private": true } functions/src/index.ts: import { HttpsError, onCall } from "firebase-functions/v2/https"; import { onSchedule } from "firebase-functions/v2/scheduler"; import { onUserCreated } from "firebase-functions/v2/auth"; import { setGlobalOptions } from "firebase-functions/v2"; // ... other imports
What I Have Tried (This is the most important part):
I have tried every possible solution for this dependency issue, but the v2 folder is never created inside node_modules/firebase-functions
.
My attempts include:
node_modules
and package-lock.json
and running npm install
.package.json
: Manually setting "firebase-functions": "^5.0.1" (as shown above).package.json
. My npm version is also up-to-date.npm cache clean --force
.firebase init
functions.
Even after all these steps, when I run npm install
, the firebase-functions
folder is created without the v2 sub-directory, which causes the tsc build to fail.My Environment:
I am trying to add new Cloud Functions (using v2 syntax) to my existing Firebase project. However, my deployment fails at the tsc build step with the error Cannot find module firebase-functions/v2/auth
.
What I expected to happen:
I expected npm install to set up my node_modules
directory correctly according to my package.json
, which would create the firebase-functions/v2 folder. Then, firebase deploy should compile my TypeScript code and deploy the functions.
What actually resulted:
The firebase-functions/v2
directory is never created inside node_modules
, which causes the build to fail every time.
My Files:
Here are my main configuration and code files:
package.json
:
{
"name": "functions",
"scripts": {
"build": "tsc",
"deploy": "firebase deploy --only functions",
"lint": "eslint --ext .js,.ts ."
},
"engines": {
"node": "18"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^12.0.0",
"firebase-functions": "^5.0.1",
"@google/generative-ai": "^0.24.1"
},
"devDependencies": {
"typescript": "^5.0.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.1.0"
},
"private": true
}
functions/srs/index.ts(relevant parts)
import { HttpsError, onCall } from "firebase-functions/v2/https";
import { onSchedule } from "firebase-functions/v2/scheduler";
import { onUserCreated } from "firebase-functions/v2/auth";
import { setGlobalOptions } from "firebase-functions/v2";
// ... other imports
What I have tried so far: I have spent a long time debugging this with an assistant and have tried every possible solution I could find:
package.json
: I manually updated my dependencies to require modern versions:
node_modules
folder and the package-lock.json
file and then run npm install
.package.json
's engines field.npm cache clean --force
.firebase init
functions, selecting TypeScript and all default options.Even after all these steps, the result is the same: npm install
installs an old version of firebase-functions that does not contain the v2 directory. This seems to be a persistent issue with my local npm environment that I cannot resolve.
I have generally checked all versions that may be compatible.
There reason is that package firebase-functions does not contain export path firebase-functions/v2/auth. There is available exports from firebase-functions package.json:
"exports": {
"./logger/compat": "./lib/logger/compat.js",
"./logger": "./lib/logger/index.js",
"./params": "./lib/params/index.js",
"./v1": "./lib/v1/index.js",
"./v1/analytics": "./lib/v1/providers/analytics.js",
"./v1/auth": "./lib/v1/providers/auth.js",
"./v1/database": "./lib/v1/providers/database.js",
"./v1/firestore": "./lib/v1/providers/firestore.js",
"./v1/https": "./lib/v1/providers/https.js",
"./v1/pubsub": "./lib/v1/providers/pubsub.js",
"./v1/remoteConfig": "./lib/v1/providers/remoteConfig.js",
"./v1/storage": "./lib/v1/providers/storage.js",
"./v1/tasks": "./lib/v1/providers/tasks.js",
"./v1/testLab": "./lib/v1/providers/testLab.js",
".": "./lib/v2/index.js",
"./core": "./lib/v2/core.js",
"./options": "./lib/v2/options.js",
"./https": "./lib/v2/providers/https.js",
"./pubsub": "./lib/v2/providers/pubsub.js",
"./storage": "./lib/v2/providers/storage.js",
"./tasks": "./lib/v2/providers/tasks.js",
"./alerts": "./lib/v2/providers/alerts/index.js",
"./alerts/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
"./alerts/billing": "./lib/v2/providers/alerts/billing.js",
"./alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js",
"./alerts/performance": "./lib/v2/providers/alerts/performance.js",
"./eventarc": "./lib/v2/providers/eventarc.js",
"./identity": "./lib/v2/providers/identity.js",
"./database": "./lib/v2/providers/database.js",
"./scheduler": "./lib/v2/providers/scheduler.js",
"./remoteConfig": "./lib/v2/providers/remoteConfig.js",
"./testLab": "./lib/v2/providers/testLab.js",
"./firestore": "./lib/v2/providers/firestore.js",
"./v2": "./lib/v2/index.js",
"./v2/core": "./lib/v2/core.js",
"./v2/options": "./lib/v2/options.js",
"./v2/https": "./lib/v2/providers/https.js",
"./v2/pubsub": "./lib/v2/providers/pubsub.js",
"./v2/storage": "./lib/v2/providers/storage.js",
"./v2/tasks": "./lib/v2/providers/tasks.js",
"./v2/alerts": "./lib/v2/providers/alerts/index.js",
"./v2/alerts/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js",
"./v2/alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js",
"./v2/alerts/performance": "./lib/v2/providers/alerts/performance.js",
"./v2/eventarc": "./lib/v2/providers/eventarc.js",
"./v2/identity": "./lib/v2/providers/identity.js",
"./v2/database": "./lib/v2/providers/database.js",
"./v2/scheduler": "./lib/v2/providers/scheduler.js",
"./v2/remoteConfig": "./lib/v2/providers/remoteConfig.js",
"./v2/testLab": "./lib/v2/providers/testLab.js",
"./v2/firestore": "./lib/v2/providers/firestore.js"
},
You may see that there is "./v1/auth" but NOT "./v2/auth".
You may have a look here - https://stackoverflow.com/a/79330843/19742146