Firebase Cloud函数在子文件夹中导入模块时未正确部署



我在部署Firebase云函数时遇到问题,因为它找不到导入的本地模块。

以下是部署命令的输出

> 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.

我尝试运行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.

正如您在下面的代码中看到的,MyModule是一个导入的本地模块。


文件夹结构(使用firebase init创建(

root
│   firebase.json
│   ...
│
└───functions
│   package.json
│   node_modules
│   ...
│
└───src
│   index.ts
└───module
│   MyModule.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

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
}

我试过

  • 测试在本地运行函数(它确实有效!(
  • 再次运行deploy命令之前,请删除Firebase控制台中的函数
  • 删除node_modules并重新运行npm install

不确定是什么原因导致了问题。为什么找不到本地模块?


更新

我已经尝试将MyModule.ts移动到与index.ts相同的级别,并且它确实进行了部署!

所以它看起来是这样的:

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;
});

但它仍然困扰着我,为什么不能从子文件夹导入模块?因为我的项目中有很多文件,我想把它们整理一下。

我想明白了。我注意到我有两个函数,它们都有相似的实现,但导入不同的模块,只有一个函数会导致部署错误。

然后我想起来,我最近将有问题的模块从myModule.ts重命名为MyModule.ts,但Visual Studio Code仍然将其视为myModule.ts,但代码仍然运行(本地(,所以我忽略了它

如果你认为你的问题和我的问题相似,你可以做以下事情。

  • 将有问题的文件重命名为其他文件
  • (如果要保留名称(删除存储库(本地(,然后再次克隆

我遇到了同样的问题,我的问题只需运行就解决了

firebase deploy --only "functions:FunctionNameHere"

相关内容

  • 没有找到相关文章

最新更新