2017-04-04 1023 views
5

我有一个npm脚本,我正在尝试调试。我使用vscode,所以我想我会创建一个调试配置,并与调试器一起通过它。使用vscode调试npm脚本的正确方法是什么?

我NPM脚本的样子是:

"scripts": { 
    ... 
    "dev": "node tasks/runner.js", 
} 

因此,我创建了以下调试配置:

{ 
    "version": "0.2.0", 
    "configurations": [ 
     { 
      "type": "node", 
      "request": "launch", 
      "name": "Launch Program", 
      "runtimeExecutable": "npm", 
      "cwd": "${workspaceRoot}", 
      "runtimeArgs": [ 
       "run", "dev" 
      ], 
      "port": 5858, 
      "stopOnEntry": true 
     } 
    ] 
} 

当我火了脚本运行,但vscode是从来没有能够连接和我得到的错误:

Cannot connect to runtime via 'legacy' protocol; consider using 'inspector' protocol (timeout after 10000 ms).

我尝试添加一个检查协议:

 { 
      "type": "node", 
      "request": "attach", 
      "name": "Attach (Inspector Protocol)", 
      "port": 9229, 
      "protocol": "inspector" 
     } 

,并通过运行该脚本故宫:

npm run dev --inspect 

这一次,我得到的错误:

Ensure Node was launched with --inspect. Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9229).

我不知道我错过了什么部分。每个重复的标记

编辑我看到其他的问题回复:通过vscode调试NPM脚本,但在其他问题的细节,答案并不详细情况和具体。如果有人正在搜索我遇到的具体vscode错误消息或我配置的类型,他们不一定会得到这个问题的答案详细信息,这个问题的选择答案给出。

+1

[如何使用npm从VSCode运行脚本进行调试?](http://stackoverflow.com/questions/34835082/how-to-debug-using-npm-run-scripts-from-vscode) –

回答

8

您不应该尝试调试npm script,因为您真正想要的是将调试器附加到使用npm run命令启动的脚本(此处仅将NPM用作任务运行器)。

{ 
    "version": "0.2.0", 
    "configurations": [ 
     { 
     "type": "node", 
     "request": "launch", 
     "name": "Launch Program", 
     "program": "${workspaceRoot}/tasks/runner.js" 
     } 
    ] 
} 

如果你真的想运行它使用NPM脚本,那么你可以使用以下配置:

{ 
    "type": "node", 
    "request": "launch", 
    "name": "Launch via NPM", 
    "runtimeExecutable": "npm", 
    "windows": { 
    "runtimeExecutable": "npm.cmd" 
    }, 
    "runtimeArgs": [ 
    "run-script", 
    "dev" 
    ], 
    "port": 5858 
} 

,但你也必须change你的脚本命令(指定调试端口)

"scripts": { 
    "dev": "node --nolazy --debug-brk=5858 tasks/runner.js" 
    }, 

只需点击齿轮图标并选择一个,即可探索各种调试配置。

enter image description here

更多关于Node.js的调试可以在VS Code documentation找到。

+0

真棒!感谢您的回复和详细的答复。我很感激。 –

相关问题