0
votes

I am a new to Node.js and Firebase. I have successfully tried to deploy some Cloud Functions to test them a bit. I have a Node.js project in which I have a class defined as:

import * as Api from './api';

export default class MyClass {
    constructor(props) {[...]}

    someFunction(props) {
        return Api.someOtherFunction(props.arg1).then([..]).catch([..]);
    }
}

In the Api code I use the firebase admin SDK and I work with the real time database.Ex.:

ref.child(`users/${userId}`).set({
            id: userId,
            arg1: arg1,
            arg2: arg2
        });

Now, the problem is that I would like to use MyClass in a cloud function. I have read a lot about ES6 in Cloud Functions, too (ex.: here), but I am not able to get rid of the error message

SyntaxError: Unexpected token import

I have tried to convert to require statements but I am not able to require my local module where MyClass is. I don't care if it will be a Node.js local module or simply some classes in clear hierarchical structure.

What I would like to ask is if there is a specific documentation about this situation (I have searched a lot for it) and/or if I am following the right way to structure my project. If the answer is "NO", please give me some tips on how to structure it.

1

1 Answers

0
votes

Cloud Functions (node.js) currently does not support the ES6 import syntax, you have to use require().

The examples in the article you linked are using Babel to convert ES6 to ES5. Even if you convert your import statements to require() you're going to encounter the fact that ES5 does not support the class syntax. If you want to do what the article it doing, follow the steps to get Babel converting your ES6 script to ES5 before sending it to Firebase Cloud.