0

I'm using node version 10.15.3, npm version 6.9.0, VS code, and firebase-functions version 2.2.0 on a windows pc. Adding async/await to my app.post() function causes this:

 Function failed on loading user code. Error message: Code in file 
 index.js can't be loaded.
 Is there a syntax error in your code?
 Detailed stack trace: /user_code/index.js:31
 app.post('/pay-now', async (req, res) => {
                       ^

 SyntaxError: Unexpected token (
     at createScript (vm.js:56:10)
     at Object.runInThisContext (vm.js:97:10)
     at Module._compile (module.js:549:28)
     at Object.Module._extensions..js (module.js:586:10)
     at Module.load (module.js:494:32)
     at tryModuleLoad (module.js:453:12)
     at Function.Module._load (module.js:445:3)
     at Module.require (module.js:504:17)
     at require (internal/module.js:20:19)
     at getUserFunction (/var/tmp/worker/worker.js:439:24)

Here's my app.post():

app.post('/pay-now', async (req, res) => {
    // charge user's card
    const charge = await makeCharge(req, res)
    // store order info in database, returns address of order
    const address = await storeOrder(req, res, charge.id)
    // send email to customer
    await emailHandler.sendCustomerEmail(req, res)
    // send email to company letting them know they have a new order
    await emailHandler.sendLTEmail(req, res, address, true)
    return res.sendStatus(200)
})

I tried removing the async & await's in app.post(), but then I get the same error at the first use of async in the function makeCharge. Any idea on what could be wrong?

4
  • have you tried deleting the node_modules directory followed by npm install? Maybe you've changed node versions recently, and your modules were built with a different version? Commented Mar 15, 2019 at 22:28
  • What exactly did you do (run a command? do something in an editor?) to make that error appear? Commented Mar 15, 2019 at 22:35
  • @Robbie just tried that and got the same result Commented Mar 15, 2019 at 23:55
  • @DougStevenson i run firebase deploy --only functions Commented Mar 15, 2019 at 23:57

2 Answers 2

1

Currently, the default runtime for Cloud Funtions is node 6, which doesn't support async/await. You will need to edit your package.json to target the node 8 runtime, which uses a version of JavaScript that does have async/await.

Set the version by adding an engines field to the package.json file that was created in your functions/ directory during initialization. For example, if you prefer to use only version 8, edit package.json to add this line:

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

If you have already deployed a version of the function, you will need to delete it and deploy again after this configuration is in place.

Sign up to request clarification or add additional context in comments.

1 Comment

For me this is not the problem. I am on v14 of node.
-1

maybe you should take a look here : Syntax for async arrow function

There is no problem with your module, I think the arrow function is not well written.

EDIT :

I don't know much about arrow function, can you try to use a "normal function" something like :

  app.post('/pay-now', async function Myfunction (req, res) {
  // do something
}

4 Comments

doesn't seem to work for me, the arrow creates a syntax error
31:48 error Parsing error: Unexpected token, expected "{" on the line app.post('/pay-now', async function (req, res) => {
Hmm, don't know then, can you not use an arrow function? And try a "normal" function? Does this work?
the answer by @DougStevenson was able to fix the problem, thanks for the help though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.