【问题标题】:Unexpected token function in Cloud9 AWS LambdaCloud9 AWS Lambda中的意外令牌函数
【发布时间】:2018-07-12 09:30:45
【问题描述】:

我正在使用节点 v8.11.3 npm v5.10.0 尝试 AWS lambda 函数中的 async/await 方法。当我运行它时,它会给我以下响应:

{
    "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)"
    ]
}

lambda 函数是:

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);
 }

如何解决这个问题?

【问题讨论】:

  • 去掉function这个词。

标签: amazon-web-services aws-lambda aws-cloud9


【解决方案1】:

您收到此错误是因为您在 cloud9 环境进程中运行的是旧版本的 nodejs(可以通过console.log(process.version) 验证,这与node --version 不同)。按照以下步骤更新 Cloud9 环境中的流程节点:

nvm install 11
nvm use 11
nvm alias default v11

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多