Firebase Async Function Syntax Error

Loading...

Firebase Async Function Syntax Error

I have node.js v8.3.0, and have this in my package.json:
  "engines": {
    "node": ">=8.3.0"
  }

And my test code for await/async:
async function x() {
    return "test";
}

exports.asyncTest = functions.https.onRequest((request, response) => async function() {
    response.end(await x());
});

Expected output: test
Observed output:
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:12
async function x() {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at getUserFunction (/var/tmp/worker/worker.js:372:24)

Solutions/Answers:

Solution 1:

The Cloud Functions for Firebase runtime is currently Node.js v6.x, so async/await is not supported. There is an expectation that Node v8.x will be supported sometime after it enters Long-Term Support (LTS).

In the meantime, you’ll need to use a transpiler like Babel or TypeScript to take advantage of async/await.

References

Loading...