VSCode:在开始调试 Web 项目时自动运行 npm 脚本

Han*_*ans 6 debugging npm visual-studio-code

这是我的 VS 代码launch.json文件:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:4321",
            "port": 9222,
            "webRoot": "${workspaceFolder}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想要的是npm start在调试开始之前运行命令,即运行开发服务器,然后启动 Chrome 实例并导航到提供的 url。

因此,我将以下代码片段添加到配置文件中并运行调试:

            "cwd": "${workspaceRoot}",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
               "start"
            ]
Run Code Online (Sandbox Code Playgroud)

但我收到了这个错误:

Attribute runtimeExecutable does not exist ('npm')
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

Han*_*ans 5

找到解决方案:我需要一个preLaunchTask

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome against localhost",
            ...
            // This runs dev server before debugger
            "preLaunchTask": "start-dev-server",
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start-dev-server",
            "type": "npm",
            "script": "start",
            "isBackground": true,
            "problemMatcher": {
                "owner": "npm",
                "background": {
                    "activeOnStart": true,
                     "beginsPattern": ".*",
                     "endsPattern": "Finished.+"
                },
                "pattern": {
                    "regexp": "",
                }
            }
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

  • 我使用了这个,但它没有完成,因此它不会返回并启动 chrome 窗口 (5认同)