![](/img/trans.png)
[英]Accessing javascript variables from the vscode debug console (with node.js running)
[英]How to debug in VSCode for javascript running 'node'
我正在使用“node myscript.js”運行我的 JS。
我嘗試在 VSCode 中使用“運行->開始調試”進行調試
我在 VSCode 中創建了一個工作區,當我看到它時創建了一個 launch.js
{
// 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": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
但是調試器失敗了
Debugger listening on ws://127.0.0.1:55596/0b6425a4-e21c-47c2-a81d-bb8e43386246
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
Process exited with code 0
我怎樣才能讓它工作?
我已經嘗試了所有這些配置。 但是它們都沒有達到我設置的斷點。 我確實在終端的 Console.log 中看到了控制台 output。
{
// 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": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"name": "Launch via NPM",
"request": "launch",
"runtimeArgs": [
"run-script",
"debug"
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"command": "npm start",
"name": "Run npm start",
"request": "launch",
"type": "node-terminal"
},
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
使用pwa-chrome
類型啟動將嘗試在 web 瀏覽器中啟動您的代碼並運行它,這根本不運行node
,因此似乎是滿足您需求的錯誤方法,並且以這種方式達到斷點更復雜。 我建議使用簡單的pwa-node
類型的啟動來代替,因為打斷點既好又容易。
第一步是通讀 vscode 的節點調試說明: https://code.visualstudio.com/docs/nodejs/nodejs-debugging
在上面的說明中,它建議使用啟動類型node
,但最近的說明建議使用pwa-node
(請參閱此處: VSCode 上的 pwa-node 類型啟動配置是什么? )。
一個簡單的.vscode/launch.json
文件,用於調試通過index.js
啟動的節點應用程序:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch my Node programme",
"skipFiles": ["<node_internals>/**"],
"cwd": "${workspaceFolder}",
"program": "index.js",
"args": [
"command-line-arguments",
"go-here",
]
}
]
}
一旦你有了啟動器,你可以在你的 js 中設置一個斷點,然后點擊調試按鈕。
可以在此處找到啟動配置屬性的詳細信息: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.