使用visual studio代码调试yeoman“无法启动程序......”

时间:2015-10-28 15:50:20

标签: debugging yeoman visual-studio-code

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

我的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
  • Yeoman ist latest

我在这里缺少什么?

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

3 个答案:

答案 0 :(得分:3)

要在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&#34; \ npm \ node_modules \ yo \ lib \ cli.js&#34;   yourGeneratorName

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

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

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

答案 1 :(得分:2)

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

enter image description here

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

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

答案 2 :(得分:1)

我偶然发现了这一点,试图找出如何调试vscode中用打字稿编写的yeoman生成器。

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

要调试一个打字稿/ yeoman模板,您需要修改vscode提供的默认启动配置,如下所示。 调试只是测试输出目录。

{
   // 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>/**"
      ],
    },
  ]
}