javascript - 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
有谁知道我在这里想念什么?
解决方案
推荐阅读
- sql-server - 在 SQL 中将长字符串转换为 ASCII
- dll - 我收到一条错误消息,提示使用 Visual Studio 2017 重建 Pocketsphinx 时未找到 .dll
- intellij-idea - 自动导入不使用关键字import,而是粘贴全限定路径
- wordpress - 在 woocommerce 上更新订单状态时在 FB Messenger 上发送消息
- c# - Ajax 发布仅返回多个选择框的第一个值
- r - 如何对值大于的数据框进行排序
- categories - 如何访问此数组中的类别以显示在我的自定义页面上
- php - CSRF:我需要对没有 cookie 的网站使用 CSRF 令牌吗?
- go - 单个函数中的多个参数类型
- python - Python/Flask:将消息和状态码返回给调用函数?