使用Visual Studio代码的0.3版本,我不知道如何启用源映射和调试ts文件
我收到以下错误 can't launch program '/Projects/app-server/server.ts'; enabling source maps might help
如何启用源映射和打字稿调试 . 在我的中,Sourcemap设置为true
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"sourceMap": true
}
}
launch.json
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch server.ts",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "server.ts",
// Automatically stop program after launch.
"stopOnEntry": true,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { }
},
{
"name": "Attach",
"type": "node",
// TCP/IP address. Default is "localhost".
"address": "localhost",
// Port to attach to.
"port": 5858
}
]
}
8 回答
这个配置对我来说很好:
项目分布
这个想法是编译
src
文件夹下的打字稿并将其放在bin
文件夹下 .tsconfig.json
激活
sourceMap
选项很重要 .launch.json
====编辑====
这是我目前在Visual Studio Code v1中使用的配置:
请注意,如果您使用任何任务运行器作为gulp,则键
preLaunchTask
非常有用,因为IDE可以按名称检测其任务 .跑步
编译
ts
(输入终端rm -r bin/ ; tsc
或执行编译任务)在可视代码中播放
Launch type
(我们的配置名称)享受!
这是我在2017年11月使用最新的TS和VsCode为我工作的原因
以下配置将帮助您启动服务器并在VS Code中调试TS
这就是我的 tsconfig.json 看起来像:
这就是我的 directory structure 看起来像:
如果你注意你会看到我的src文件夹和构建文件夹(包含结果转换的JS和 Map 文件)并存,这真的有助于我维护一个逻辑目录结构 .
好的,现在来了 launch config :
您可以使用您想要使用的任何preLaunchTask,甚至可以跳过它 . 如果你跳过它,你必须手动将TS转换为JS .
这就是我在任务中所做的事情
nb-tsc-watch
下面的设置用断点测试摩卡柴 . 它的工作原理是将src转换为lib目录,然后在lib中运行测试,并将sourceMapping运行回src .
.vscode/launch.json
tsconfig.json
.vscode/tasks.json
package.json 显示已安装的模块 . 脚本与调试无关 .
Mac OSX 10.12.3 Sierra
Visual Studio Code 1.10.1
NodeJS v7.7.1
对于2017年2月更晚版本的VSCode,这对我有用(它是@manu和@tommy Falgout提供的组合):
它假设您的json输出文件分别位于 dest 文件夹中,而您的源文件位于 src 文件夹中
/.vscode/launch.json
tsconfig.json
我认为它比发布更简单,更简单......
我已经安装了
ts-node
,所以我的配置文件最终非常简单......launch.json
有两个值得注意的事情:
runtimeArgs
- 传递给节点以注册ts节点以处理TypeScript文件 .没有
program
属性 . 要启动的TS文件的名称将作为第一个参数给出 .这样你就不需要将TS编译成JS,你甚至可以将用TS编写的模块编译成JS .
@manu的回答指出了我正确的方向;但是,使用最新版本的VSCode,我仍然遇到了同样的问题 . 这是对我有用的修复:
https://github.com/Microsoft/vscode/issues/13499
2017年12月17日
.vscode / launch.json```json
.vscode / tasks.json
tsconfig.json
我刚刚编写了自己的PowerShell函数作为preLaunchTask . 这可能是比以前更糟的解决方案,但可以在preLaunchTask字段下添加更多灵活性来注入更多任务 . 因为目前它不支持数组,并且只允许在启动操作之前运行一个任务 .
launch.json
tasks.json
RebuildTypeScript.ps1