2

I am trying the async/await approach in AWS lambda function with node v8.11.3 npm v5.10.0. When I run it gives me the following response:

{
    "errorMessage": "Unexpected token function",
    "errorType": "SyntaxError",
    "stackTrace": [
        "                        ^^^^^^^^",
        "SyntaxError: Unexpected token function",
        "createScript (vm.js:56:10)",
        "Object.runInThisContext (vm.js:97:10)",
        "Module._compile (module.js:542:28)",
        "Object.Module._extensions..js (module.js:579:10)",
        "Module.load (module.js:487:32)",
        "tryModuleLoad (module.js:446:12)",
        "Function.Module._load (module.js:438:3)",
        "Module.require (module.js:497:17)",
        "require (internal/module.js:20:19)"
    ]
}

The lambda function is:

const fetch = require('node-fetch')
exports.handler = async function(event,context)
{
 console.log(event);

 let img = await 
 fetch(`https://catappapi.herokuapp.com/users/${event.userId}`);
 let parseddata = await img.json()
 console.log(parseddata.imageUrl);
 }

How to solve this issue?

Abhishek Jain
  • 445
  • 2
  • 6
  • 19

1 Answers1

0

You are getting this error because you are running an older version of nodejs in cloud9 environment process (can verify that by console.log(process.version) which would be different than node --version). Follow these steps to update the process node in your Cloud9 environment:

nvm install 11
nvm use 11
nvm alias default v11
ttfreeman
  • 5,076
  • 4
  • 26
  • 33