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

时间:2019-12-28 09:35:57

标签: 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}"
        }
    ]
}

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

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

            "cwd": "${workspaceRoot}",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
               "start"
            ]

但是我得到了这个错误:

Attribute runtimeExecutable does not exist ('npm')

有帮助吗?

1 个答案:

答案 0 :(得分:0)

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

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome against localhost",
            ...
            // This runs dev server before debugger
            "preLaunchTask": "start-dev-server",
        }
    ]
}

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": "",
                }
            }
        },
    ]
}