简体   繁体   English

Firebase 在子文件夹中导入模块后,云功能未正确部署

[英]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.我在部署 Firebase Cloud Functions 时遇到问题,因为它找不到导入的本地模块。

Here's the output of deploy command这是部署命令的 output

> 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.我尝试运行firebase --debug deploy但它没有提供有用的信息,而是将我发送到 Firebase 控制台中的日志。

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.正如您在下面的代码中看到的, MyModule是一个导入的本地模块。


Folder Structure (created using firebase init )文件夹结构(使用firebase init创建)

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

index.ts索引.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 ./module/MyModule.ts

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

package.json 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!)在本地测试运行 function(它确实有效!)
  • Delete the function in Firebase Console before running the deploy command again再次运行deploy命令前先删除Firebase Console中的function
  • Delete node_modules and run npm install again删除node_modules ,再次运行npm install

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!我试图将MyModule.ts移动到与index.ts相同的级别并且它确实部署了!

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.我注意到我有 2 个函数都有类似的实现,但导入了不同的模块,只有 1 个函数会导致部署错误。

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.然后我想起我最近将有问题的模块从myModule.ts重命名为MyModule.ts但 Visual Studio Code 仍然将其视为myModule.ts但代码仍然运行(本地)所以我忽略了它。

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云函数部署错误 - 超出配额 - Firebase Cloud Functions Deploy Error - Quota Exceeded React + Firebase Cloud Functions in Typescript部署失败 - React + Firebase Cloud Functions in Typescript fails to deploy Firebase 云函数部署错误 - SyntaxError: Unexpected token '?' - Firebase Cloud Functions Deploy Error- SyntaxError: Unexpected token '?' 使用“firebase deploy”部署到托管 - firebase 如何知道它是部署到托管而不是云功能? - using "firebase deploy" to deploy to hosting - how does firebase know if it is deploying to hosting and not cloud functions? Firebase如何在不影响其他一些功能的情况下将一些功能部署到Cloud Functions? - How to deploy some functions to Cloud Functions for Firebase without affecting some other functions? 如何在 firebase 云函数上正确使用 express-session - How to properly use express-session on firebase cloud functions 如何在 firebase 云函数中正确地将 signInWithEmailAndPassword() 转换为 createCustomToken() - Node.js - How to properly convert signInWithEmailAndPassword() to createCustomToken() in firebase cloud functions - Node js 使用云构建部署云功能失败:错误:未能获得 firebase 项目 - deploy cloud functions using cloud build failed: Error: failed to get firebase project 如何使用 Firebase Cloud Functions.npmrc 设置私有 NPM 模块? - How To Setup Private NPM Module With Firebase Cloud Functions .npmrc? Firebase 函数部署失败 - Firebase functions failed to deploy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM