42

Looks like Cloud Functions does not support Async-Await notation. Is there a way I could use Babel until they do or is it recommended to use promises?

My current function that sits on Node is like so:

exports.getToken = async (req, res) => {
  //1. Generate token from Braintree
  const result = await gateway.clientToken.generate();

  //2. Return the client token
  res.json(result.clientToken);
};
Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
Brandon
  • 587
  • 1
  • 4
  • 6

6 Answers6

46

Cloud Functions runs the LTS version of node.js, which according to the documentation is 6.14.0 at this moment in time. node 6.x supports EcmaScript 6, which does not include async/await.

However, you can write your code in TypeScript and have that transpiled down to ES5/ES6, which will effectively convert the use of async/await into promises. A web search suggests that perhaps this plugin can be used to help Babel with similar transpiling.

It's worth noting that the Firebase CLI now allow you to initialize a new Cloud Functions project with native TypeScript support, which is what the Firebase team is currently recommending to developers.

If you don't want to use TypeScript, you can now also choose node 8 (which is currently in beta, and does support async/await for plain JavaScript) as a deployment target. You can follow the documentation to edit your package.json to indicates that your functions should be deployed to node 8.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
  • It's also easy to use babel to compile your ES8 code to ES6 code that works with Cloud Functions, see example here: https://codeburst.io/es6-in-cloud-functions-for-firebase-2-415d15205468 – chez.mosey May 03 '18 at 09:32
  • thank you, solved my issue but, the whole branch is deleted if all children are old values >> can I keep branches empty of not? – Moustafa EL-Saghier Sep 13 '18 at 21:07
  • 3
    Plus 1 for setting "engine": {"node":8}. Such a quick fix! Wish I could +100. – Rap Nov 08 '18 at 14:51
39

Now you can use Node.js version 8 by adding this in your functions/package.json:

"engines": {
   "node": "8"
}

Example: https://github.com/firebase/functions-samples/blob/Node-8/authenticated-json-api/functions/package.json

l2aelba
  • 18,585
  • 20
  • 90
  • 131
7

Instead of transpile TypeScript, I have transpiled my javascript after follow this very nice post and take a look at this repository

Basically you can do:

npm install -g @babel/cli @babel/core @babel/preset-env
  • UPDATE:
    I'm having troubles with version "7.0.0-beta.51" of babel. "7.0.0-beta.44" still ok.
    Switch to stable version 6

    npm install --save-dev babel-cli babel-preset-env

Create the file .babelrc inside your project folder

{
  "presets": [
    ["@babel/env", {
      "targets": {
        "node": "6.11.5"
      }
    }]
  ]
}

Move your "functions" folder to "firebaseFunctions" folder and then run

babel firebaseFunctions --out-dir functions --copy-files --ignore firebaseFunctions/node_modules

Or run this command for each files you want to transpile

babel originalfile.js --out-file transpiledfile.js
rma
  • 137
  • 1
  • 8
3

The above solutions didn't work for me alone. I had to update to the latest firebase tools:

npm update -g firebase-tools

and then update my package.json with adding:

 "engines": {"node": "8"}

and everything worked fine with async/await.

Credits to this blog https://howtofirebase.com/cloud-functions-migrating-to-node-8-9640731a8acc

Adam
  • 2,973
  • 25
  • 22
0

In your functions/.eslintrc.json file set as 'ecmaVersion': 2017 this will remove eslint syntax error

"parserOptions": {
    "ecmaVersion": 2017
 },

In your functions/package.json file set node version to 8 by adding below

"engines": {
     "node": "8"
},

this will update the cloud node version to 8 default node version is 6

NuOne
  • 4,218
  • 1
  • 27
  • 39
0

as @adam said, solved my problem to reinstall/upgrade the global firebase package

the difference is in my case was using NVM (node version manager). Somehow, my default node(v13.x) had the firebase-tools but i didnt installed globally at project node (v10/8) so first:

nvm use 10

then:

npm i -g firebase-tools

reinstalling at correct node version got my async functions working properly.