简体   繁体   中英

Firebase Cloud Functions did not deploy properly with imported a module in subfolder

I have a problem deploying Firebase Cloud Functions because it can't find the imported local module.

Here's the output of deploy command

> firebase deploy --only functions
> build
> tsc

✔  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (69.03 KB) for uploading
i  scheduler: ensuring required API cloudscheduler.googleapis.com is enabled...
i  pubsub: ensuring required API pubsub.googleapis.com is enabled...
✔  scheduler: required API cloudscheduler.googleapis.com is enabled
✔  pubsub: required API pubsub.googleapis.com is enabled
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 12 function myFunctionName(us-central1)...
i  functions: scheduler job firebase-schedule-myFunctionName-us-central1 is up to date, no changes required
✔  functions[myFunctionName(us-central1)]: Successful upsert schedule operation. 

Functions deploy had errors with the following functions:
        myFunctionName(us-central1)

To try redeploying those functions, run:
    firebase deploy --only "functions:myFunctionName"

To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.

I tried running firebase --debug deploy but it does not provide useful information and sends me to Logs in Firebase Console instead.

Provided module can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module './module/MyModule'
Could not load the function, shutting down.

Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Function cannot be initialized.

As you can see in the code below MyModule is an imported local module.


Folder Structure (created using firebase init )

root
│   firebase.json
│   ...
│
└───functions
    │   package.json
    │   node_modules
    │   ...
    │
    └───src
        │   index.ts
        └───module
            │   MyModule.ts

index.ts

import * as functions from "firebase-functions";
import { MyClass } from "./module/MyModule";

export const myFunctionName = functions.pubsub
  .schedule("every 5 minutes")
  .onRun(async () => {
    const myClass = new MyClass();
    const result = await myClass.doSomething();
    functions.logger.log("something has done!", result);
    return result;
  });

./module/MyModule.ts

export class MyClass {
  async doSomething() {
    // Do Something
  }
}

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "googleapis": "^80.1.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

I have tried

  • Test running the function locally (it does work!)
  • Delete the function in Firebase Console before running the deploy command again
  • Delete node_modules and run npm install again

Not sure what caused the problem. Why can't find a local module?


Update

I have tried to move MyModule.ts to the same level as index.ts and it does deploy!

So it will look like this:

root
│   firebase.json
│   ...
│
└───functions
    │   package.json
    │   node_modules
    │   ...
    │
    └───src
        │   index.ts
        │   MyModule.ts
import * as functions from "firebase-functions";
import { MyClass } from "./MyModule"; // Updated import

export const myFunctionName = functions.pubsub
  .schedule("every 5 minutes")
  .onRun(async () => {
    const myClass = new MyClass();
    const result = await myClass.doSomething();
    functions.logger.log("something has done!", result);
    return result;
  });

But it still bothers me why can't import a module from a subfolder? Because there's a lot of files in my project and I want to organize them.

I figured it out. I noticed that I have 2 functions both have similar implementation but importing a different module and only 1 function causes a deployment error.

And then I remembered that I have recently renamed the problematic module from myModule.ts to MyModule.ts but Visual Studio Code still sees it as myModule.ts but the code still runs (locally) so I ignored it.

If you think your problem is similar to mine you can do the following.

  • Rename the problematic file to something else.
  • (If you want to keep the name) Delete your repository (locally) and clone it again.

I had the same problem mine was fixed by simply running:

firebase deploy --only "functions:FunctionNameHere"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM