0

In a React Native project using Expo, I was trying to deploy the following cloud function using export:

Note: I use Javascript in the index.js.

export const helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

But I got this error:

Error: Error occurred while parsing your function triggers.

/Users.../functions/index.js:5
export const helloWorld = functions.https.onRequest((request, response) => {
^^^^^^

SyntaxError: Unexpected token export
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)

Then I used exports.helloWorld... and it worked fine!

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

Can anyone explain why this happened?

Thanks

Fotios Tsakiris
  • 1,310
  • 1
  • 18
  • 24
  • 1
    https://stackoverflow.com/questions/38296667/getting-unexpected-token-export – Doug Stevenson Apr 01 '20 at 07:21
  • Hey @DougStevenson thanks for the reply. As I see: _"NodeJS uses CommonJS Module syntax (module.exports) not ES6 module syntax (export keyword)"._ But why does it work with TypeScript in your tutorials? Does the code get transpiled in commonjs with babel when it compiles it to Javascript in the lib folder? – Fotios Tsakiris Apr 01 '20 at 07:37
  • TypeScript is always transpiled, and you have to choose the module system that it uses. Firebase selects the correct one for you in tsconfig.json when it creates your project. You can see what it generates by looking at the javascript code under lib. – Doug Stevenson Apr 01 '20 at 15:52

1 Answers1

1

change it to:

const helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

module.exports = helloWorld

direct exports make the export one of exports. ie: refer by exports.something

Module.exports assign export directly where refered.

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • Hey @Nilanka thanks for the answer. I'll follow your suggestion! – Fotios Tsakiris Apr 01 '20 at 07:39
  • Actually I had problems with deploying the functions when using `module.exports = ...`. Pls check my comment [here](https://stackoverflow.com/questions/49017318/firebase-function-deploying-but-not-showing-up-in-console) – Fotios Tsakiris Apr 15 '20 at 14:00