[英]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我努力了
node_modules
and run npm install
againnode_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.如果您认为您的问题与我的类似,您可以执行以下操作。
I had the same problem mine was fixed by simply running:我遇到了同样的问题,只需运行即可解决:
firebase deploy --only "functions:FunctionNameHere"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.