1

I'm facing a persistent deployment issue after refactoring my Cloud Functions from a single large index.js file into an organized folder structure as recommended in the documentation. The deployment consistently fails with a "Cannot find module" error.

My Setup:

  • firebase-tools: Latest version
  • firebase-functions: v4.4.1
  • node: v20

Project Structure:

backend/
├── functions/
│   ├── src/
│   │   ├── index.js  <-- Main entry point
│   │   ├── http/
│   │   │   └── myHttpFunction.js
│   │   └── pubsub/
│   │       └── myPubSubFunction.js
│   └── package.json
└── firebase.json

My package.json (functions/package.json):

{
  "name": "functions",
  "main": "src/index.js",
  "engines": { "node": "20" },
  ...
}

My firebase.json (backend/firebase.json):

{
  "functions": {
    "source": "functions"
  },
  ...
}

My index.js (functions/src/index.js):

This file loads and re-exports all functions from their individual files.

const admin = require("firebase-admin");
admin.initializeApp();

exports.myHttpFunction = require('./http/myHttpFunction').myHttpFunction;
exports.myPubSubFunction = require('./pubsub/myPubSubFunction').myPubSubFunction;
// ... and so on for all functions

The Error

When I run

firebase deploy --only functions

the deployment fails for every single function with the error:

Error: Cannot find module './http/myHttpFunction'
Require stack: /workspace/src/index.js

This clearly indicates that the src directory is not being uploaded to the Cloud Functions environment, and the require calls in the cloud-side index.js fail.

Things I Have Tried Without Success:

  • "files" entry in package.json: Adding "files": ["src/"] to package.json did not solve the issue.

  • Changing "main" entry: I've confirmed "main" points to src/index.js.

  • Changing firebase.json: I've tried both the object syntax ("functions": { "source": "functions" }) and the array syntax ("functions": [{ "source": "functions", ... }]), neither worked.

  • Checking firebase-debug.log: The debug log shows that the CLI packages the project, but it seems to ignore the src subdirectory during the upload.

  • Permissions: All my user and service account permissions are correct (Owner, Cloud Functions Admin, etc.). This is a file packaging issue, not an IAM issue.

It seems the Firebase CLI's deployment packaging is not correctly including the src directory when the entry point is nested. Has anyone encountered this specific issue and found a reliable configuration fix? I'm trying to avoid deploying 50+ functions one by one from a single, constantly changing index.js file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.