节点运行代码但不会在 vscode 中的断点处停止
node runs code but does not stop at breakpoints in vscode
我在 node.js
中有简单的测试代码,我在 windows 10
上 运行 中 vscode
。
我在第一行代码下了断点
let x = 'hello world'
console.log(x)
我的launch.json
配置如下
"version": "0.2.0",
"configurations": [
// nodes
{
"type": "node",
"request": "launch",
"name": "node: Launch Program",
"program": "${file}",
"console": "integratedTerminal",
},
{
"type": "node",
"request": "launch",
"name": "node: Launch Program (External Terminal)",
"program": "${file}",
"console": "externalTerminal"
}
]
集成终端和外部终端中的代码运行s,但如果我设置一个不会在断点处停止。
这是我在集成终端中看到的:
C:\Users\Owner\Dropbox\programming\javascript> cd c:\Users\Owner\Dropbox\programming\javascript && cmd /C "set "NODE_OPTIONS=--require C:\Users\Owner\AppData\Local\Temp\vscode-js-debug-bootloader.js" && set "VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"\\.\pipe\node-cdp.24028-42.sock","deferredMode":false,"waitForDebugger":"","execPath":"C:\Program Files\nodejs\node.exe","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"C:\Users\Owner\AppData\Local\Temp\node-debug-callback-101745418fdd2bc6"}" && "C:\Program Files\nodejs\node.exe" .\test.js "
Debugger listening on ws://127.0.0.1:52096/7c733fea-7053-4446-8901-4220611d16ad
For help see https://nodejs.org/en/docs/inspector
hello world
我已经尝试了各种配置,但none似乎可行。我必须假设我没有调用调试器。我所知道的是,在执行类似操作时会为 python
调用调试器。
请问如何让代码在断点处停止?
没有提供解决方案,所以这是迄今为止最好的解决方案。
经过检查,调试器确实启动了。
运行这个示例代码:
let x = 'hello world'
let SomeSize = 1000
for (let i = 0; i < SomeSize; i++) {
console.log(x, i)
}
console.log('the end')
输出结果如下:
hello world 525
hello world 526
hello world 527
Debugger attached.
hello world 528
hello world 529
hello world 530
所以可以看出调试器实际上确实附加了,但是在大约 500 行代码之后已经 运行(在这种特殊情况下)。
如果程序很短,比如 100 个循环,用户可能没有机会检查代码 运行 是否结束,因为 breakpoint
会在 debugger
附上自己。
后续问题可能是如何延迟调试器以防止这种滞后行为,答案可能在这里:https://nodejs.org/api/debugger.html
或者,使用 setTimeout
延迟代码,让调试器有机会启动。
已编辑:解决方案是升级到最新版本的 node:
node -v v15.4.0
我在 node.js
中有简单的测试代码,我在 windows 10
上 运行 中 vscode
。
我在第一行代码下了断点
let x = 'hello world'
console.log(x)
我的launch.json
配置如下
"version": "0.2.0",
"configurations": [
// nodes
{
"type": "node",
"request": "launch",
"name": "node: Launch Program",
"program": "${file}",
"console": "integratedTerminal",
},
{
"type": "node",
"request": "launch",
"name": "node: Launch Program (External Terminal)",
"program": "${file}",
"console": "externalTerminal"
}
]
集成终端和外部终端中的代码运行s,但如果我设置一个不会在断点处停止。
这是我在集成终端中看到的:
C:\Users\Owner\Dropbox\programming\javascript> cd c:\Users\Owner\Dropbox\programming\javascript && cmd /C "set "NODE_OPTIONS=--require C:\Users\Owner\AppData\Local\Temp\vscode-js-debug-bootloader.js" && set "VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"\\.\pipe\node-cdp.24028-42.sock","deferredMode":false,"waitForDebugger":"","execPath":"C:\Program Files\nodejs\node.exe","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"C:\Users\Owner\AppData\Local\Temp\node-debug-callback-101745418fdd2bc6"}" && "C:\Program Files\nodejs\node.exe" .\test.js "
Debugger listening on ws://127.0.0.1:52096/7c733fea-7053-4446-8901-4220611d16ad
For help see https://nodejs.org/en/docs/inspector
hello world
我已经尝试了各种配置,但none似乎可行。我必须假设我没有调用调试器。我所知道的是,在执行类似操作时会为 python
调用调试器。
请问如何让代码在断点处停止?
没有提供解决方案,所以这是迄今为止最好的解决方案。
经过检查,调试器确实启动了。
运行这个示例代码:
let x = 'hello world'
let SomeSize = 1000
for (let i = 0; i < SomeSize; i++) {
console.log(x, i)
}
console.log('the end')
输出结果如下:
hello world 525
hello world 526
hello world 527
Debugger attached.
hello world 528
hello world 529
hello world 530
所以可以看出调试器实际上确实附加了,但是在大约 500 行代码之后已经 运行(在这种特殊情况下)。
如果程序很短,比如 100 个循环,用户可能没有机会检查代码 运行 是否结束,因为 breakpoint
会在 debugger
附上自己。
后续问题可能是如何延迟调试器以防止这种滞后行为,答案可能在这里:https://nodejs.org/api/debugger.html
或者,使用 setTimeout
延迟代码,让调试器有机会启动。
已编辑:解决方案是升级到最新版本的 node:
node -v v15.4.0