【问题标题】:VSCode: Automatically run npm script on starting debugging of a web projectVSCode:在开始调试 Web 项目时自动运行 npm 脚本
【发布时间】:2019-12-28 09:35:57
【问题描述】:

这是我的 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}"
        }
    ]
}

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

所以,我在配置文件中添加了以下 sn-p 并运行调试:

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

但我得到了这个错误:

Attribute runtimeExecutable does not exist ('npm')

有什么帮助吗?

【问题讨论】:

    标签: debugging npm visual-studio-code


    【解决方案1】:

    找到解决方案:我需要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": "",
                    }
                }
            },
        ]
    }
    

    【讨论】:

    • 我用过这个,但它没有完成,因此它不会返回并启动 chrome 窗口
    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2022-01-02
    • 2015-06-19
    • 2017-02-01
    相关资源
    最近更新 更多