首页 > 解决方案 > 将firebase云功能拆分为不同文件的正确方法是什么?

问题描述

我正在尝试将大型云功能拆分为更小更易于维护的文件。但是,在部署时,我总是遇到同样的错误:

Error occurred while parsing your function triggers.

/Users/Raphi/Documents/Programing/Development/merchantAPI/functions/index.js:2
import { DirectPost } from '../functions/merchantPost';
^^^^^^

SyntaxError: Cannot use import statement outside a module

我昨晚大部分时间都在谷歌上搜索,但似乎没有任何文档

简化的 index.js:

import { DirectPost } from '../functions/merchantPost';
exports.portal = functions.https.onRequest(async (request, response) => {
  var charge = new DirectPost()
})

简化的merchantPost.js:

export class DirectPost {
  constructor(){}
}

标签: javascriptnode.jsgoogle-cloud-functions

解决方案


尝试在 Node.js 中包含来自另一个文件的 JavaScript 类定义

还可以考虑为“门户处理程序”回调制作一个单独的文件,这样你就可以做到

exports.portal = functions.https.onRequest(portalHandler);

它使你的 index.js 更干净。

还可以考虑使用 TypeScript(其中导入的东西完全按照您在代码中尝试的方式完成),从长远来看,这将通过防止一些讨厌的错误来帮助您......


推荐阅读