首页 > 解决方案 > SyntaxError: Unexpected token ( 在 Azure Function 中引用 npm 包时

问题描述

我正在尝试创建一个基于 JavaScript 的调用 webhint API 的 Azure 函数。我已经成功创建了函数app,一个函数,初始化了一个包并安装了hint npm包。在我的功能代码中,我正在执行以下操作:

var hint = require('hint');

module.exports = function (context, myTimer) {
    var webhint = hint.Analyzer.create({}, {});
    var result = webhint.analyze('https://google.com', {});
    context.done();
};

运行函数应用程序时,我在控制台中看到以下错误:

mscorlib: Exception while executing function: Functions.WebHintFunction. mscorlib: One or more errors occurred. C:\projects\webhint-function-app\WebhintFunction\WebHintFunction\node_modules\hint\dist\src\lib\engine.js:133
                    immediateId = setImmediate(async () => {
                                                     ^

SyntaxError: Unexpected token (
    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 Object.<anonymous> (C:\projects\webhint-function-app\WebhintFunction\WebHintFunction\node_modules\hint\dist\src\lib\index.js:6:10).

错误来自提示,但我认为我的函数版本、节点版本或 javascript 版本有问题。我尝试将以下内容添加到local.settings.json文件中,但没有任何运气:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_EXTENSION_VERSION": "~3",
    "WEBSITE_NODE_DEFAULT_VERSION": "~12"
  }
}

更新:正如评论中所建议的,我尝试使函数异步:

module.exports = async function(context, myTimer) {
    const webhint = hint.Analyzer.create({}, {});
    const result = await webhint.analyze('https://google.com', {});
    context.done();
};

这引入了一个新问题:

mscorlib: Exception while executing function: Functions.WebHintFunction. mscorlib: One or more errors occurred. C:\projects\webhint-function-app\WebhintFunction\WebHintFunction\index.js:3
module.exports = async function run(context, myTimer) {
                       ^^^^^^^^

SyntaxError: Unexpected token function

有谁知道我在这里想念什么?

标签: javascriptazure-functionswebhint

解决方案


推荐阅读