What is the correct way to split firebase cloud functions into different files?

我怕爱的太早我们不能终老 提交于 2021-01-28 09:50:52

问题


I'm trying to split a large cloud function into a smaller more maintainable files. However when deploying I always get the same error:

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

I spent most of last night googling but there doesn't seem to be any documentation for this

simplified index.js:

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

simplified merchantPost.js:

export class DirectPost {
  constructor(){}
}

回答1:


Try Including JavaScript class definition from another file in Node.js.

Also consider making a separate file for the "portal handler" callback, so that you can do

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

It makes your index.js much cleaner.

Also consider using TypeScript (in which importing stuff is done exactly as you're trying to in your code) which will help you in the long run by preventing some nasty bugs...



来源:https://stackoverflow.com/questions/60871924/what-is-the-correct-way-to-split-firebase-cloud-functions-into-different-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!