vscode调试babel-node 2

2019-08-17  本文已影响0人  心情后花园

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

1. babel-node 调试 node

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

1.1. 配置参数

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"
    }
  ]
}
上一篇 下一篇

猜你喜欢

热点阅读