如何在 visual studio 代码中调试打字稿文件
how to debug typescript files in visual studio code
使用 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
}
]
}
这个配置对我来说工作正常:
项目分布
|-- .vscode
|----- launch.json
|-- bin
|----- app.js
|----- app.js.map
|-- src
|----- app.ts
|-- node_modules
|-- [..]
|-- tsconfig.json
|-- [...]
想法是在 src
文件夹下编译打字稿并将其放在 bin
文件夹下。
tsconfig.json
激活 sourceMap
选项很重要。
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"module": "commonjs",
"target": "ES5",
"outDir": "bin",
"rootDir": "src",
"sourceMap": true
}
}
launch.json
==== 编辑 ====
这是我目前在 Visual Studio 代码 v1:
中使用的配置
{
"version": "0.2.0",
"configurations": [
{
"args": [],
"cwd": "${workspaceRoot}",
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"name": "DEBUG",
"outDir": "${workspaceRoot}/bin",
"preLaunchTask": "compile",
"program": "${workspaceRoot}/src/app.ts",
"request": "launch",
"runtimeArgs": [
"--nolazy"
],
"runtimeExecutable": null,
"sourceMaps": true,
"stopOnEntry": false,
"type": "node"
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
请注意,如果您将任何任务运行程序用作 gulp,键 preLaunchTask
将非常有用,因为 IDE 能够通过名称检测其任务。
运行
- 编译您的
ts
(在终端中输入 rm -r bin/ ; tsc
或执行您的编译任务)
- 在可视化代码中播放
Launch type
(我们的配置名称)
- 尽情享受吧!
@manu 的回答为我指明了正确的方向;但是,使用最新版本的 VSCode,我仍然遇到同样的问题。这是对我有用的修复:
https://github.com/Microsoft/vscode/issues/13499
"outFiles": [ "${workspaceRoot}/js/*.js" ]
对于 VSCode 的更高版本 Feb/2017,这对我有用(它是@manu 和 @tommy Falgout 提供的组合):
它假定您的 json 输出文件位于 dest 文件夹中,您的源文件分别位于 src 文件夹中
/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch",
"sourceMaps": true,
"stopOnEntry": true,
"console": "internalConsole",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/src/main.ts",
"outFiles": ["${workspaceRoot}/dest/*.js"]
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": []
}
]
}
tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"module": "commonjs",
"outDir": "dest",
"rootDir": "src"
},
"exclude": [
"node_modules"
]
}
以下设置使用断点测试 mocha chai。它的工作原理是将 src 转译到 lib 目录,然后 运行 在 lib 中使用 sourceMapping 测试回 src。
.vscode/launch.json
{
"type": "node",
"request": "launch",
"preLaunchTask": "tsc",
"name": "Run Mocha",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": ["lib/**/*.js"],
"cwd": "${workspaceRoot}",
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/lib/**/*.js"]
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"sourceMap": true,
"outDir": "lib",
"target": "es6"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
package.json 显示已安装的模块。脚本与调试无关。
"scripts": {
"test": "mocha --compilers ts:ts-node/register src/**/*.spec.ts",
"test:coverage": "nyc -e '.ts' npm test"
},
"dependencies": {
"@types/chai": "^3.4.35",
"@types/mocha": "^2.2.39",
"@types/node": "^7.0.5",
"@types/sinon": "^1.16.35",
"chai": "^3.5.0",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"sinon": "^1.17.7",
"ts-node": "^2.1.0",
"typescript": "^2.2.1"
}
- Mac OSX 10.12.3 塞拉
- Visual Studio 代码 1.10.1
- NodeJS v7.7.1
我刚刚将自己的 PowerShell 函数编写为 preLaunchTask。这可能是比以前的解决方案更糟糕的解决方案,但可以增加更多的灵活性以在 preLaunchTask 字段下注入更多任务。因为目前不支持数组,只允许一个task在launch action前运行
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node.js",
"program": "${file}",
"preLaunchTask": "RebuildTypeScript",
"outFiles": [
"${workspaceRoot}/js/**/*.js"
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"taskName": "RebuildTypeScript",
"type": "shell",
"command": "Powershell ./RebuildTypeScript.ps1",
"group": "none",
"presentation": {
"reveal": "never"
}
}
]
}
RebuildTypeScript.ps1
$currentDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
function CompileTypeScriptFiles($folder) {
$tsFiles = Get-ChildItem $folder -Filter "*.ts" -Recurse
$tsFiles | ForEach-Object {
$tsFile = $_.FullName;
$options = $tsFile + " --outDir js --sourceMap"
Start-Process "tsc" $options
}
}
CompileTypeScriptFiles $currentDir
截至 2017 年 11 月,这就是我使用最新 TS 和 VsCode 的方法
以下配置将帮助您在 VS Code 中启动服务器和调试 TS
这是我的 tsconfig.json 的样子:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2017", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "../build",
"sourceMap": true,
"target": "es2016",
"typeRoots": [
"../node_modules/@types"
]
},
"include": [
"**/*.ts"
],
"exclude": [
"../node_modules",
"../*.js"
]
}
这就是我的 目录结构 的样子:
如果你注意的话,你会看到我的 src 文件夹和 build 文件夹(包含生成的转译 JS 和地图文件)并存,这确实有助于我保持逻辑目录结构。
好的,启动配置:
{
"type": "node",
"request": "launch",
"name": "Start and Debug",
"preLaunchTask": "nb-tsc-watch",
"timeout": 10000,
"program": "${workspaceFolder}/backend/src/app.ts",
"restart": true,
"cwd": "${workspaceRoot}",
"outFiles": [
"${workspaceFolder}/backend//build/**/*.js"
],
"sourceMaps": true
}
您可以使用任何您想使用的 preLaunchTask,甚至可以跳过它。
如果你跳过它,你将不得不手动将 TS 转译为 JS。
这就是我在任务中所做的nb-tsc-watch
{
"label": "nb-tsc-watch",
"type": "typescript",
"tsconfig": "backend/src/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
]
}
2017/12/17
.vscode/launch.json
```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": [
{
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/index.ts",
"outFiles": [
"${workspaceRoot}/dist/index.js"
],
"sourceMaps": true,
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"preLaunchTask": "compile",
"name": "DEBUG"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
]
}
.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
tsconfig.json
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "dist",
"rootDir": "src"
},
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
我认为它在发布后变得越来越简单...
我已经安装了 ts-node
(https://github.com/TypeStrong/ts-node),所以我的配置文件非常简单。
在项目文件夹中使用 npm install ts-node --save-dev
安装 ts-node
- 感谢评论中的 Hrafnkell
launch.json
{
"name": "Launch index.ts",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
]
}
有两点值得注意:
runtimeArgs
- 传递给节点以注册 ts-node 来处理 TypeScript 文件。
- 没有
program
属性。要启动的 TS 文件的名称改为作为第一个参数给出。
这样就不需要把TS编译成JS,甚至可以把用TS写的模块还没有编译成JS
对我来说,经过这么多 launch.json 配置。我发现使用 jestJs 和 istanbul 会导致我的断点不会在正确的位置中断,直到我将配置设置为:
config.collectCoverage = false;
如果您 运行 从命令行运行脚本,在最新的 Visual Studio 代码版本中,您可以跳过 launch.json
的创建,这有时是一项艰巨的任务。相反,您可以从命令行自动将调试器附加到任何 ts-node
或 node
命令,您 运行。
- 为您的
tsconfig.json
启用源映射 - TypeScript 配置需要源映射支持,否则无法进行调试。
{
"compilerOptions": {
"sourceMap": true
},
}
- 在 Visual Studio 代码调试器上启用 自动附加。它是任务栏上的一个按钮,但也可以通过命令面板访问。
- 而不是将脚本启动为:
ts-node myscript.ts
启动为
node -r ts-node/register --inspect-brk myscript.ts
您将在节点启动时看到:
Debugger listening on ws://127.0.0.1:9229/8bb6dcc8-a73c-405e-b1fe-69a3d7789a20
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
然后Visual Studio代码调试器将
停在程序第一行
在 Visual Studio 代码编辑器中设置的以下任意断点处停止
自动配置方法
对于许多用例来说,一个简单的自动配置就足够了——无需手动配置 launch.json
。先决条件:在工作区 tsconfig.json
:
中启用 sourcemaps
{
"compilerOptions": {
"sourceMap": true,
// ...
}
}
1.) 调试当前文件without launch.json
只需打开或重新聚焦文件,然后按 F5(开始调试)。如果存在多个调试环境,如 Chrome 和 Node.js,select 后者。
注意:目前这不需要在 launch.json
中出现其他条目。下一个 VS Code 版本将附带 single file debug improvements.
2.) 自动创建 launch.json
转到调试视图 (CtrlShiftD) 并单击 "create a launch.json file".如果 main
不存在,这将为 package.json
或活动文件的 main
字段文件创建一个调试条目。示例:
"configurations": [ // auto-generated
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\dist\A.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
注意:这要求 launch.json
之前不存在。
3.) 将调试器附加到 运行 程序
在 settings.json
或通过 UI → "Debug: Toggle Auto Attach" 开启 Auto Attach feature。
"debug.node.autoAttach": "on" // in settings.json
以调试模式启动节点程序。不久之后,VS Code 将开始调试。
node --inspect-brk dist/A.js
或使用"Debug: Attach to Node Process"(也可使用launch.json
:"${command:PickProcess}"
)。
如果您不想硬编码文件名但喜欢简单 version here ? Using info from VS variable reference page 您可以做两件事:
npm i ts-node
然后launch.json like(以防万一,但你只能从中获取这一个配置):
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch TS",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/${fileBasename}"
]
}
]
}
该 VSC 页面中的几个示例 - 有时您可以在某个地方使用 Ctrl+Space 来获取它们,但在我现有的内部不起作用:
${workspaceFolder} - /home/your-username/your-project
${workspaceFolderBasename} - your-project
${file} - /home/your-username/your-project/folder/file.ext
${relativeFile} - folder/file.ext
${relativeFileDirname} - folder
${fileBasename} - file.ext
${fileBasenameNoExtension} - file
${fileDirname} - /home/your-username/your-project/folder
${fileExtname} - .ext
${lineNumber} - line number of the cursor
${selectedText} - text selected in your code editor
${execPath} - location of Code.exe
使用 visual studio 代码的 0.3 版,我不确定如何启用源映射和调试 ts 文件
我收到以下错误can't launch program '/Projects/app-server/server.ts'; enabling source maps might help
如何启用源映射和打字稿调试。 Sourcemap 在我的
中设置为 truetsconfig.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
}
]
}
这个配置对我来说工作正常:
项目分布
|-- .vscode
|----- launch.json
|-- bin
|----- app.js
|----- app.js.map
|-- src
|----- app.ts
|-- node_modules
|-- [..]
|-- tsconfig.json
|-- [...]
想法是在 src
文件夹下编译打字稿并将其放在 bin
文件夹下。
tsconfig.json
激活 sourceMap
选项很重要。
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"module": "commonjs",
"target": "ES5",
"outDir": "bin",
"rootDir": "src",
"sourceMap": true
}
}
launch.json
==== 编辑 ====
这是我目前在 Visual Studio 代码 v1:
中使用的配置{
"version": "0.2.0",
"configurations": [
{
"args": [],
"cwd": "${workspaceRoot}",
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"name": "DEBUG",
"outDir": "${workspaceRoot}/bin",
"preLaunchTask": "compile",
"program": "${workspaceRoot}/src/app.ts",
"request": "launch",
"runtimeArgs": [
"--nolazy"
],
"runtimeExecutable": null,
"sourceMaps": true,
"stopOnEntry": false,
"type": "node"
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
请注意,如果您将任何任务运行程序用作 gulp,键 preLaunchTask
将非常有用,因为 IDE 能够通过名称检测其任务。
运行
- 编译您的
ts
(在终端中输入rm -r bin/ ; tsc
或执行您的编译任务) - 在可视化代码中播放
Launch type
(我们的配置名称) - 尽情享受吧!
@manu 的回答为我指明了正确的方向;但是,使用最新版本的 VSCode,我仍然遇到同样的问题。这是对我有用的修复:
https://github.com/Microsoft/vscode/issues/13499
"outFiles": [ "${workspaceRoot}/js/*.js" ]
对于 VSCode 的更高版本 Feb/2017,这对我有用(它是@manu 和 @tommy Falgout 提供的组合):
它假定您的 json 输出文件位于 dest 文件夹中,您的源文件分别位于 src 文件夹中
/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch",
"sourceMaps": true,
"stopOnEntry": true,
"console": "internalConsole",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/src/main.ts",
"outFiles": ["${workspaceRoot}/dest/*.js"]
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": []
}
]
}
tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"module": "commonjs",
"outDir": "dest",
"rootDir": "src"
},
"exclude": [
"node_modules"
]
}
以下设置使用断点测试 mocha chai。它的工作原理是将 src 转译到 lib 目录,然后 运行 在 lib 中使用 sourceMapping 测试回 src。
.vscode/launch.json
{
"type": "node",
"request": "launch",
"preLaunchTask": "tsc",
"name": "Run Mocha",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": ["lib/**/*.js"],
"cwd": "${workspaceRoot}",
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/lib/**/*.js"]
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"sourceMap": true,
"outDir": "lib",
"target": "es6"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
package.json 显示已安装的模块。脚本与调试无关。
"scripts": {
"test": "mocha --compilers ts:ts-node/register src/**/*.spec.ts",
"test:coverage": "nyc -e '.ts' npm test"
},
"dependencies": {
"@types/chai": "^3.4.35",
"@types/mocha": "^2.2.39",
"@types/node": "^7.0.5",
"@types/sinon": "^1.16.35",
"chai": "^3.5.0",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"sinon": "^1.17.7",
"ts-node": "^2.1.0",
"typescript": "^2.2.1"
}
- Mac OSX 10.12.3 塞拉
- Visual Studio 代码 1.10.1
- NodeJS v7.7.1
我刚刚将自己的 PowerShell 函数编写为 preLaunchTask。这可能是比以前的解决方案更糟糕的解决方案,但可以增加更多的灵活性以在 preLaunchTask 字段下注入更多任务。因为目前不支持数组,只允许一个task在launch action前运行
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node.js",
"program": "${file}",
"preLaunchTask": "RebuildTypeScript",
"outFiles": [
"${workspaceRoot}/js/**/*.js"
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"taskName": "RebuildTypeScript",
"type": "shell",
"command": "Powershell ./RebuildTypeScript.ps1",
"group": "none",
"presentation": {
"reveal": "never"
}
}
]
}
RebuildTypeScript.ps1
$currentDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
function CompileTypeScriptFiles($folder) {
$tsFiles = Get-ChildItem $folder -Filter "*.ts" -Recurse
$tsFiles | ForEach-Object {
$tsFile = $_.FullName;
$options = $tsFile + " --outDir js --sourceMap"
Start-Process "tsc" $options
}
}
CompileTypeScriptFiles $currentDir
截至 2017 年 11 月,这就是我使用最新 TS 和 VsCode 的方法
以下配置将帮助您在 VS Code 中启动服务器和调试 TS
这是我的 tsconfig.json 的样子:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2017", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "../build",
"sourceMap": true,
"target": "es2016",
"typeRoots": [
"../node_modules/@types"
]
},
"include": [
"**/*.ts"
],
"exclude": [
"../node_modules",
"../*.js"
]
}
这就是我的 目录结构 的样子:
如果你注意的话,你会看到我的 src 文件夹和 build 文件夹(包含生成的转译 JS 和地图文件)并存,这确实有助于我保持逻辑目录结构。
好的,启动配置:
{
"type": "node",
"request": "launch",
"name": "Start and Debug",
"preLaunchTask": "nb-tsc-watch",
"timeout": 10000,
"program": "${workspaceFolder}/backend/src/app.ts",
"restart": true,
"cwd": "${workspaceRoot}",
"outFiles": [
"${workspaceFolder}/backend//build/**/*.js"
],
"sourceMaps": true
}
您可以使用任何您想使用的 preLaunchTask,甚至可以跳过它。 如果你跳过它,你将不得不手动将 TS 转译为 JS。
这就是我在任务中所做的nb-tsc-watch
{
"label": "nb-tsc-watch",
"type": "typescript",
"tsconfig": "backend/src/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
]
}
2017/12/17
.vscode/launch.json
```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": [
{
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/index.ts",
"outFiles": [
"${workspaceRoot}/dist/index.js"
],
"sourceMaps": true,
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"preLaunchTask": "compile",
"name": "DEBUG"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
]
}
.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
tsconfig.json
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "dist",
"rootDir": "src"
},
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
我认为它在发布后变得越来越简单...
我已经安装了 ts-node
(https://github.com/TypeStrong/ts-node),所以我的配置文件非常简单。
在项目文件夹中使用 npm install ts-node --save-dev
安装 ts-node
- 感谢评论中的 Hrafnkell
launch.json
{
"name": "Launch index.ts",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
]
}
有两点值得注意:
runtimeArgs
- 传递给节点以注册 ts-node 来处理 TypeScript 文件。- 没有
program
属性。要启动的 TS 文件的名称改为作为第一个参数给出。
这样就不需要把TS编译成JS,甚至可以把用TS写的模块还没有编译成JS
对我来说,经过这么多 launch.json 配置。我发现使用 jestJs 和 istanbul 会导致我的断点不会在正确的位置中断,直到我将配置设置为:
config.collectCoverage = false;
如果您 运行 从命令行运行脚本,在最新的 Visual Studio 代码版本中,您可以跳过 launch.json
的创建,这有时是一项艰巨的任务。相反,您可以从命令行自动将调试器附加到任何 ts-node
或 node
命令,您 运行。
- 为您的
tsconfig.json
启用源映射 - TypeScript 配置需要源映射支持,否则无法进行调试。
{
"compilerOptions": {
"sourceMap": true
},
}
- 在 Visual Studio 代码调试器上启用 自动附加。它是任务栏上的一个按钮,但也可以通过命令面板访问。
- 而不是将脚本启动为:
ts-node myscript.ts
启动为
node -r ts-node/register --inspect-brk myscript.ts
您将在节点启动时看到:
Debugger listening on ws://127.0.0.1:9229/8bb6dcc8-a73c-405e-b1fe-69a3d7789a20
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
然后Visual Studio代码调试器将
停在程序第一行
在 Visual Studio 代码编辑器中设置的以下任意断点处停止
自动配置方法
对于许多用例来说,一个简单的自动配置就足够了——无需手动配置 launch.json
。先决条件:在工作区 tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true,
// ...
}
}
1.) 调试当前文件without launch.json
只需打开或重新聚焦文件,然后按 F5(开始调试)。如果存在多个调试环境,如 Chrome 和 Node.js,select 后者。
注意:目前这不需要在 launch.json
中出现其他条目。下一个 VS Code 版本将附带 single file debug improvements.
2.) 自动创建 launch.json
转到调试视图 (CtrlShiftD) 并单击 "create a launch.json file".如果 main
不存在,这将为 package.json
或活动文件的 main
字段文件创建一个调试条目。示例:
"configurations": [ // auto-generated
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\dist\A.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
注意:这要求 launch.json
之前不存在。
3.) 将调试器附加到 运行 程序
在 settings.json
或通过 UI → "Debug: Toggle Auto Attach" 开启 Auto Attach feature。
"debug.node.autoAttach": "on" // in settings.json
以调试模式启动节点程序。不久之后,VS Code 将开始调试。
node --inspect-brk dist/A.js
或使用"Debug: Attach to Node Process"(也可使用launch.json
:"${command:PickProcess}"
)。
如果您不想硬编码文件名但喜欢简单
npm i ts-node
然后launch.json like(以防万一,但你只能从中获取这一个配置):
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch TS",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/${fileBasename}"
]
}
]
}
该 VSC 页面中的几个示例 - 有时您可以在某个地方使用 Ctrl+Space 来获取它们,但在我现有的内部不起作用:
${workspaceFolder} - /home/your-username/your-project
${workspaceFolderBasename} - your-project
${file} - /home/your-username/your-project/folder/file.ext
${relativeFile} - folder/file.ext
${relativeFileDirname} - folder
${fileBasename} - file.ext
${fileBasenameNoExtension} - file
${fileDirname} - /home/your-username/your-project/folder
${fileExtname} - .ext
${lineNumber} - line number of the cursor
${selectedText} - text selected in your code editor
${execPath} - location of Code.exe