node.js - 调试时 "Run-Script"如何在 VS Code 中为 node.js 应用程序工作?

标签 node.js visual-studio-code

screen

我手动添加了一个配置 start:debug 但随后 VS Code 又显示了另一个配置。两者都执行应用程序,但是当我运行我的应用程序时,它不会在终端中显示所有应用程序控制台输出,例如错误、日志等,但是当我运行 VS Code 时一切正常,我更喜欢在我们的团队中使用该配置。

问题是我无法 checkin 配置,所以在另一台机器上它没有按预期显示。 VS Code 如何获取该配置并执行它?如果我可以在我的配置中复制它,那么我可以在我的存储库中检查它以供其他人使用。

最佳答案

以下是解开谜团的步骤:按照以下步骤,您将发现难以捉摸的选项的任务配置设置,并发现它是如何添加到您的列表中的:

  1. 创建一个空文件夹(我将我的文件夹命名为 so-70196209 在此问题 ID 之后),然后在新的 VS Code 工作区中打开它。

  2. 在文件夹中创建一个package.json 文件。确保它有一个像这样的 start:debug 脚本条目:

    package.json:

    {
      "name": "so-70196209",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "start:debug": "echo \"Success\"",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "MIT"
    }
    
  3. 在 VS Code 菜单中,选择“运行”>“添加配置...”

    run > add configuration

  4. 在出现的列表中,选择“Node.js”:

    node.js

    .vscode/launch.json 中的文件将使用如下默认任务创建:

    .vscode/launch.json:

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "type": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "program": "${file}"
        }
      ]
    }
    

    You can delete this default task later if you don't want to keep it, but just leave it for now and follow along to the end.

  5. Activity Bar 中选择“运行和调试”图标.

  6. 在“运行和调试”边栏中,选择下拉菜单并选择“Node.js...”:

    node.js

  7. 在出现的列表中,找到带有文本“Run Script: start:debug”的条目。找到右侧的齿轮图标,然后选择齿轮

    If you hover over the gear, a tooltip will appear with the text "Edit Debug Configuration in launch.json"

    run script: start:debug

    这将向 .vscode/launch.json 添加一个新条目,这个条目就是您一直在搜索的条目。 (它不在您的启动配置中但在您的下拉列表中的原因是因为您之前在某个时候单击了输入行,而不是齿轮。我不知道为什么这会将它添加到下拉列表中并且不是配置,但这就是它现在的工作方式。)

    配置文件现在看起来像这样:

    .vscode/launch.json:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "program": "${file}"
        },
        {
          "type": "node-terminal",
          "name": "Run Script: start:debug",
          "request": "launch",
          "command": "npm run start:debug",
          "cwd": "${workspaceFolder}"
        }
      ]
    }
    

    “运行和调试”下拉菜单现在有您想要的条目:

    run and debug dropdown menu

问题解决了! 🥳

关于node.js - 调试时 "Run-Script"如何在 VS Code 中为 node.js 应用程序工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70196209/

相关文章:

node.js - 在 Visual Studio Code (vscode) 中禁用 node.js 应用程序调试

javascript - Appium:更改 Android DatePicker 值 Javascript/NodeJS

javascript - 表达 app.use 刷新行为

visual-studio-code - 如何让 Prettier、Eslint 和 Visual Studio Code 一起玩得很好

c# - 我可以从 Visual Studio Code 编译并运行 .NET MVC 5 项目吗?

javascript - Node.js回调,等待函数结束

node.js - 创建同步类别和 channel

visual-studio-code - VSCode :/usr/bin/node: bad option: --inspect since last upgrade?

node.js - 使用 require ('' 将模块消耗到 VS Code 扩展中时出现问题)

powershell - VSCode Powershell 集成终端启动时挂起