Reputation: 597
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);
};
Upvotes: 43
Views: 27783
Reputation: 31
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.
Upvotes: 0
Reputation: 3914
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
Upvotes: 3
Reputation: 6558
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
Upvotes: 1
Reputation: 22167
Now you can use Node.js version 8 by adding this in your functions/package.json
:
"engines": {
"node": "8"
}
Upvotes: 39
Reputation: 317467
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.
Upvotes: 46
Reputation: 177
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
Upvotes: 7