npm scripts 启动 debug

579 阅读1分钟

原文地址: How to debug using npm run scripts from VSCode?

It appears that VS Code will support npm scripts and other launch scenarios since the release from October 2016.

Below is an example as it was proposed on GitHub but has been adjusted to use --inspect-brk instead of the originally prposed --debug-brk option.

自 2016 年 10 月发布以来,VS Code 似乎将支持 npm 脚本和其他启动场景。 下面是一个示例,它是在 GitHub 上提出的,但已被调整为使用 --inspect-brk 而不是最初提出的 --debug-brk 选项。

方式1:packages.json

"scripts": {
    "debug": "node --nolazy --inspect-brk=5858 myProgram.js"
  },

方式2:vscode launch config

{
    "name": "Launch via NPM",
    "type": "node",
    "request": "launch",
    "cwd": "${workspaceRoot}",
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "run-script", "debug"
    ],
    "port": 5858
}

More recent updates for Visual Studio Code as of Summer of 2022 show that there are various ways to set up debugging support with Node.js. These methods include auto attaching to either processes started from the VSCode Integrated Terminal or the JavaScript debug terminal, or by using the traditional launch config which is now extensively documented.

截至 2022 年夏季,Visual Studio Code 的最新更新表明,有多种方法可以使用 Node.js 设置调试支持。这些方法包括自动附加到从 VSCode 集成终端或 JavaScript 调试终端启动的进程,或者使用现在已广泛记录的传统启动配置。

创建启动配置之后,当在控制台输入时便会进入debug模式

npm run debug