【问题标题】:Can't Debug Azure JavaScript Function using Visual Studio Code无法使用 Visual Studio Code 调试 Azure JavaScript 函数
【发布时间】:2022-02-11 09:01:42
【问题描述】:

我第一次使用 VS 代码并尝试使用 JavaScript 语言创建一个 azure 函数。 我关注了this article

环境:

  • NodeJS v16.x 已安装
  • 已安装 Azure Functions 扩展
  • 已安装 Azure Functions Core Tools 4.x

问题:

我创建了一个示例 azure 函数。使用 f5 运行应用程序时显示以下错误

我从host.json 中删除了扩展包配置并再次尝试。这一次,我得到了以下错误

谁能帮帮我

更新:

host.json

    {
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node"
  }
}

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
}

【问题讨论】:

  • 以管理员身份运行?
  • @AndrewCorrigan 你的意思是管理员模式下的 vs 代码?抱歉没有检查。我会检查并确认。
  • @AndrewCorrigan 我试过但结果相同

标签: javascript node.js azure visual-studio-code azure-functions


【解决方案1】:

这是我按照问题中给出的相同文档使用 Visual Studio Code 运行 JavaScript Stack 的 Azure Functions 的解决方法

先决条件:

  1. 安装 VS Code 并安装 Azure 帐户Azure Functions strong>、Azure 存储Azurite、扩展菜单中的扩展和从 VS Code 登录到 Azure 帐户。
  2. 下载并安装 NodeJS 第 14 版(我猜第 16 版正在预览中),来源:https://nodejs.org/en/download/https://phoenixnap.com/kb/install-node-js-npm-on-windows
  1. 通过单击独立安装程序从here 安装 Azure 存储模拟器(已弃用但仍用于本地开发和测试)。 - 安装 Azure 存储模拟器后,确保它正在运行。如果它已关闭,则手动运行它。 启动 Azure 存储模拟器:

    • 选择开始按钮或按Windows键。
    • 开始输入Azure Storage Emulator
    • 从显示的应用程序列表中选择模拟器。
    • 输入Start > 进入并关闭窗口。
  2. source 安装 Azure Functions Core Tools v3 和 v4 用于旧版本和新版本堆栈开发,并使用 @987654338 检查版本来自命令提示符的@n 命令。

  3. source 安装Azure CLI,从source 安装PowerShell 模块

  4. 在进入 IDE(VS Code/Visual Studio)之前,请关注 prerequisite check


在有/没有调试的情况下运行 Azure Functions 的步骤:

  1. 在文件资源管理器中创建了工作区文件夹,并从该文件夹路径打开了 VS Code。
  2. 使用 Azure Functions 扩展创建了 JavaScript Stack 的 Azure Functions 项目。 这是未修改的样板代码:

Index.js

module.exports = async  function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');

const  name = (req.query.name || (req.body && req.body.name));

const  responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

context.res = {
// status: 200, /* Defaults to 200 */
body:  responseMessage
};
}

local.settings.json

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "node"
}
}

host.json

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
  • 无需从host.json 中删除扩展包

要运行函数,请转到运行菜单> 选择Start DebuggingRun without Debugging 中的任一选项,我在没有启动VS Code 的情况下运行了它,它在本地运行成功。

调试代码:

在代码的任意行设置断点,然后从Run 菜单中选择Start Debugging 选项

注意: 对于像:A host error has occurred during startup operationValue cannot be null 这样的错误,解决步骤很少:

  • Azure 存储模拟器用于本地开发和测试目的。
  • 确保您拥有 Azure Functions Core Tools 的最新版本以及旧版本(您可以安装多个版本的 AF Core Tools 并根据要求)。
  • local.settings.json中,AzureWebJobsStorage的值默认为空,但如果我们在本地使用项目,最好使用@ 987654350@

您可以从我的GitHub Repository下载以上项目。


【讨论】:

  • 谢谢。只有我创建的方式相同。唯一的区别是“AzureWebJobsStorage”:“UseDevelopmentStorage=true”。我正在考虑任何设置问题。我从笔记本电脑上试过。但是我的桌面也遇到了同样的问题......安装vs代码后安装nodejs有什么问题吗?
  • @Akhil,检查并让我知道是否出现任何问题,并确保 host.json 与我得到的相同。
  • 谢谢。我只创建了相同的方式,但没有任何 azure webjobsstorage
  • @HariKrishnaRajoli-MT 我现在正在做的事情。我做了多次但查看环境变量
  • @HariKrishnaRajoli-MT 我可以看到 Azure Functions Extension Bundle 未添加到用户位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
  • 1970-01-01
  • 2017-08-11
  • 2017-12-18
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多