vscode调试babel-node 2

上一篇写了怎么通过babel-node 调试node,但只是粗略的说明,这段时间有了更多的了解,遂再写一篇说明下。

1. babel-node 调试 node

调试的配置文件在于 launch.json 文件中

1.1. 配置参数

  • name: 启动方式的名称(可随意写)
  • runtimeExecutable:通过这个去启动程序,路径可以是相对路径。
  • runtimeArgs:运行时的参数,可选。
  • program:程序文件入口
  • console:程序日志打印的地方。有三个选择
    1. internalConsole:vscode DEBUG CONSOLE 处打印
    2. integratedTerminal:vscode 内置的 terminal 处打印
    3. externalTerminal:第三方终端处打印。可在 vscode 中配置。mac 是在settings 里面搜索 osx exec,可以配置

1.2. 通过程序入口文件

{
    "type": "node",
    "request": "launch",
    "name": "dev",
    "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
    "program": "${workspaceFolder}/src/app.js"
}

1.3. 通过 npm 启动

虽然是通过 npm,但还需要 package.json 里面的 scripts 配置。

// scripts
{
    "scripts": {
        "dev": "nodemon --exec babel-node src/app.js | bunyan -o short -L"
    }
}

launch.json 的配置如下

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "npm1",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run",
        "dev"
      ],
      "console": "externalTerminal"
    }
  ]
}

但是这样,容易出现以下错误cannot connect to runtime process timeout after 10000 ms - (reason cannot connect to the target: connect econnrefused

解决这个错误的话,只需要在 scripts 里的命令中加上 --inspect,即下面的 debug

// scripts
{
    "scripts": {
        "dev": "nodemon --exec babel-node src/app.js | bunyan -o short -L",
        "debug": "nodemon --exec babel-node --inspect src/app.js | bunyan -o short -L"
    }
}

2. 完整配置

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "npm1",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run",
        "debug"
      ],
      "console": "externalTerminal",
      "port": 9230
    },
    {
      "type": "node",
      "request": "launch",
      "name": "dev",
      "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
      "program": "${workspaceFolder}/src/app.js"
    }
  ]
}

你可能感兴趣的:(vscode调试babel-node 2)