如何用Visual Studio代码debugging任意/随机脚本

我通常使用Webstorm,但经过Webstorm几年的成功,我有一些奇怪的问题,所以我尝试了VSCode – 我想能够debugging脚本 – 我有大约60或70个Node.js脚本我经常跑步 如果我可以避免,我不想为每个configuration单独configuration。

我总是可以使用Chromedebugging器工具,通过这样做:

node --inspect-brk my-script.js 

但我正在寻找一种直接使用VSCode IDE的方式,而不是打开Chrome浏览器。 Webstorm有一个很好的function,你可以直接在IDE中进行debugging,而且我也是99%的VSCode。

但我不能为了我的生活弄清楚如何debugging一个简单的Node.js脚本。 以下是我看到的选项:

在这里输入图像描述

然后这个:

在这里输入图像描述

我是“启动程序”的银行,但这是我得到的错误:

在这里输入图像描述

这里是launch.jsonconfiguration文件,任何人都知道我可以设置这个,所以我可以debugging任何脚本,我想?

 { // 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": "Launch Program", "program": "${workspaceFolder}/${alex_debug_script}" }, { "type": "node", "request": "attach", "name": "Attach by Process ID", "processId": "${command:PickProcess}" }, { "type": "node", "request": "attach", "name": "Attach by Process ID", "processId": "${command:PickProcess}" }, { "type": "node", "request": "attach", "name": "Attach", "port": 9229 }, { "type": "chrome", "request": "launch", "name": "Launch Chrome", "url": "http://localhost:8080", "webRoot": "${workspaceRoot}" }, { "type": "chrome", "request": "launch", "name": "Launch Chrome against localhost", "url": "http://localhost:8080", "webRoot": "${workspaceRoot}" } ] } 

正如你所看到的,我尝试使用一个envvariables${alex_debug_script}但是这并不像你在上面截图中的红色错误信息中看到的那样工作。

最后,我们有这个观点, global launch settings

在这里输入图像描述

我会诚实的,Webstorm使这一点更容易,但我有耐心!

这是你怎么做的,这是在文档中

https://code.visualstudio.com/docs/editor/debugging

它说:

 Global Launch Configuration We support adding a "launch" object inside your user settings. This "launch" configuration will then be shared across your workspaces. For example: "launch": { "version": "0.2.0", "configurations": [{ "type": "node", "request": "launch", "name": "Launch Program", "program": "${file}" }] } 

复制该对象,并将其放在您的本地/工作区launch.json文件中:

像这样:

  { "type": "node", "request": "launch", "name": "Launch Node.js file", "program": "${file}" }, 

现在当你select这个:

在这里输入图像描述

它将debugging在编辑器中select的文件。

这不是非常理想的,因为我认为我们都希望只是右键点击我们想debugging的文件,所以如果有人知道如何做,请告知。

所以我可以弄清楚如何debugging一个预定义的脚本,我将其作为launch.json中的一个configuration来添加:

  { "type": "node", "request": "launch", "name": "FooBarBaz", "program": "${workspaceFolder}/test/src/dev/node/1.test.js" }, 

现在,当我使用下拉菜单时,我看到:

在这里输入图像描述

但问题是,我不想为60或70个不同的脚本做这个,这是愚蠢的。 我应该能够做的只是右键点击我想debugging的任何脚本,并debugging该死的东西。

有人知道吗? 这只是答案的一半。