使用 visual studio 代码 "cannot launch programm ..." 调试 yeoman

debug yeoman with visual studio code "cannot launch programm ..."

我正在尝试在 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 向我打招呼并询问我想要哪个生成器 运行。如果我 select 生成器也能正常工作。

我在这里错过了什么?

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

要在 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 代码

附加到进程

node --debug "\npm\node_modules\yo\lib\cli.js" yourGeneratorName

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

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

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

在 Visual Studio 代码的最新版本中(我使用 1.16.1),您可以通过 "Add configuration".

直接添加 Node.js Yeoman 调试配置

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

中描述的路径

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

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

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

要调试 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>/**"
      ],
    },
  ]
}