我对 Typescript 和 deno 以及 vscode 都是新手。
首先用vscode尝试失败,然后用IntelliJ尝试。 我可以使用 deno 插件在 Intellij 中运行和调试简单的 .ts 文件。
但我想知道如何在 vscode 中执行此操作(还安装了 deno 插件, https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno )。
我的设置是...
.vscode/settings.json
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
.vscode/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": [
{
"request": "launch",
"name": "Launch Program",
"type": "pwa-node",
"program": "${workspaceFolder}/main.ts",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--unstable",
"--inspect",
"--allow-all"
],
"attachSimplePort": 9229
}
]
}
示例代码
console.log("Hello, World!");
当我使用运行而不调试
来运行它时
仅显示如下图所示,但预计输出 Hello, World!
。
而且调试似乎也不起作用。
请帮忙。
最佳答案
使用--inspect
进行调试不适用于相对较短的程序,因为它不会等待调试器连接。所以运行短程序时the execution finishes before VS Code connects to the inspector 。对于较大的程序,这不会成为问题。
为了调试只有几行长的 Deno 程序,给出了两个选项 in this thread :
- 使用
--inspect-brk
而不是--inspect
,这应该为调试器提供足够的时间来连接并命中断点 - 在调试时给代码添加一些时间延迟,例如:
const _sleep = async () => {
await new Promise(resolve => setTimeout(resolve, 2000));
}
await _sleep();
console.log("Hello World!"); <-- Here, you could add a breakpoint
要在不调试的情况下运行,您还可以添加 --inspect-brk
,但 session 开始后您必须按 F5 或单击“继续”。或者,您可以使用 --inspect
并添加 sleep
函数
关于visual-studio-code - 我可以在 VSCode 中运行和调试 deno 项目吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72183157/