问题
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