【问题标题】:debug yeoman with visual studio code "cannot launch programm ..."使用 Visual Studio 代码“无法启动程序...”调试 yeoman
【发布时间】:2015-10-28 15:50:19
【问题描述】:

我正在尝试在 Visual Studio 代码中调试一个 yeoman 生成器,但它一直告诉我它 cannot launch programm d:\repos\generator\node_modules\.bin\yo'; enabling source maps might help 每次我按 F5 时

我的 VS Code 配置文件如下所示:

{
    "version": "0.1.0",
    "configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Launch app/index.js",
            // Type of configuration. Possible values: "node", "mono".
            "type": "node",
            // Workspace relative or absolute path to the program.
            "program": "node_modules/.bin/yo",
            // Command line arguments passed to the program.
            "args": [ "design" ],
            // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
            "cwd": ".",
            // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
            "runtimeExecutable": null,
            // Optional arguments passed to the runtime executable.
            "runtimeArgs": ["--nolazy"],
            // Environment variables passed to the program.
            "env": {
                "NODE_ENV": "development"
            },
            // Use JavaScript source maps (if they exist).
            "sourceMaps": false,
            // If JavaScript source maps are enabled, the generated code is expected in this directory.
            "outDir": null
        }
    ]
}

我的 package.json 是这样的:

{
    "name": "generator-design",
    "version": "0.1.0",
    "main": "app/index.js",
    "files": [
        "generators/app"
    ],
    "dependencies": {
        "yeoman-generator": "^0.20.3",
        "yosay": "^1.0.5",
        "chalk": "^1.1.1",
        "uuid": "^2.0.1",
        "yeoman-option-or-prompt": "^1.0.2"
    }
}

路径正确并且 yeoman 正在工作,因为当我将其复制到命令行时,yeoman 会向我打招呼并询问我要运行哪个生成器。如果我选择它,生成器也可以正常工作。

  • VS 代码版本为0.9.2
  • 操作系统是 Windows 8.1
  • 约曼主义者latest

我在这里错过了什么?

不确定这是否相关,但是当我将 .js 添加到 yo 文件时,VS Code 会启动控制台(当然它会失败,但至少控制台会启动) 如果我指向错误的路径,错误消息将更改为program 'd:\foo\bar\yo' does not exist

【问题讨论】:

标签: debugging yeoman visual-studio-code


【解决方案1】:

要在 Visual Studio 代码中调试 Yeoman 应用程序,您必须提供 cli.js 路径而不是 yo 路径。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "<global path to yo>/node_modules/yo/lib/cli.js",
            "stopOnEntry": false,
            "args": [
                "yourGeneratorName"
            ],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        }
    ]
}

使用此系统,您无法回答任何问题。 为了与控制台交互,您必须从命令行启动调试器,然后使用 Visual Studio Code 附加到进程

节点 --debug "\npm\node_modules\yo\lib\cli.js" 你的发电机名称

在您的launch.json 上,您必须有这样的条目

{
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outDir": null,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }

建议在您的代码上放置debugger; 指令以停止程序流并等待附加。

【讨论】:

  • 嗨,Max,我目前不在生成器上工作,但是一旦我们重新访问它,我会试一试。这适用于所有节点脚本还是只适用于 yeoman?
  • 嗨,VIsual Studio Code 已经集成了 nodejs 调试工具。 Attach 方法适用于需要附加到辅助进程的任何节点 pgm。
  • “使用这个系统你无法回答任何问题”:将externalConsole 设置为true 怎么样?不是解决了这个问题吗?
  • 还有,谈论第一个代码:我可以以某种方式更改起始目录吗?当我开始调试我的生成器时,它想在我的 yeoman 项目目录中生成代码。搞得一团糟……
【解决方案2】:

在 Visual Studio Code 的最新版本(我使用 1.16.1)中,您可以通过“添加配置”直接添加 Node.js Yeoman 调试配置。

然后您只需编辑生成器名称。正如 Max 已经提到的,程序路径必须是您在 yo/lib 文件夹中的 cli.js。你可以按照in the Yeoman docs的描述找到路径。

当您启动生成器时,会启动一个内部终端,以便您与问题提示进行交互("console": "integratedTerminal"

【讨论】:

  • 有人做过吗?我正在尝试使用它,但我似乎无法让它不覆盖工作目录。我已经尝试过制作workdirs并更改cwd,但是当我开始调试时,它似乎仍然将生成器的输出写入git工作目录。
【解决方案3】:

我在试图弄清楚如何在 vscode 中调试用 typescript 编写的 yeoman 生成器时偶然发现了这一点。

虽然这不是最初的问题,但我想我会在这里发布,以防其他人有兴趣做同样的事情。

要调试 typescript / yeoman 模板,您需要将 vscode 提供的默认启动配置修改为如下所示。 其中 debug-out 只是一个测试输出目录。

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
  "version": "0.2.0",
  "configurations": [
    {
      // Debug Yeoman generator written in typescript
      "name": "Debug Generator",
      "runtimeArgs": ["${workspaceFolder}/node_modules/yo/lib/cli.js"],
      "program": "${file}",
      "cwd": "${workspaceFolder}/debug-out",
      "request": "launch",
      "type": "pwa-node",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "skipFiles": [
        "<node_internals>/**"
      ],
    },
  ]
}

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2023-01-30
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2018-07-27
    • 2023-03-15
    相关资源
    最近更新 更多