VsCode’s Debug mode supports direct debugging of Ts source files using sourceMap files while running compiled files
Debugging
The effect
As shown in the figure, when debugging the test.js file is compiled, the final effect is Debug Ts source code, so that we can see the effect of the code running more directly. If there is any error, you can directly modify the source code, rather than through the compiled JS file, separated by a layer
This is ideal for developing and debugging libraries that eventually provide a Node command to the user, such as Next (which itself uses this debugging method).
To debug Typescript source files directly, there are two steps
Step 1: Always generate a Map file when compiling Typescript
Step 2: execute the compiled js file in Vscode debug mode
The first step:Ts
Compile the supplementaryMap
file
If you want to use VsCode’s Debug mode to Debug Typescript files directly, you need to set “sourceMap”: true, and map files must be generated at compile time
With the Map file, Debug mode can find the source through the compiled file and make breakpoints set in the source take effect
You can also run TS file debugging directly using TS-Node
Test.js - test.d.ts - test.js.mapCopy the code
Step 2: Start debugging
Start from the command line
VsCode provides a debugging Terminal. In the Terminal selection box, select Create JavaScript Debug Terminal. After creating the debugging Terminal, run the Node command directly to start the debugging mode
configurationlaunch.json
File to start the
Create the launch.json configuration file and enable Debug mode
{
"version": "0.2.0"."configurations": [{"name": "launch".// Name of the debug command, which is displayed in the debug drop-down window
"type": "node".// Debug type, depending on the language, JavaScript is usually node
"request": "launch".// Launch type: launch in debug mode; Attach: Attach to an already started process enable debug mode and debug
"program": "${workspaceRoot}/lib/test"."cwd": "${workspaceRoot}"."protocol": "inspector"}}]Copy the code
launch.json
The configuration properties
-
Commonly used configuration
name
:debug
The command name is displayed in the Debug drop – down windowtype
: Debug type, depending on the language,JavaScript
As a general rule, benode
request
: Debug startup type:launch
: it is todebug
Mode start debugging;attach
: is attached to the start of an already started processdebug
Mode and debugprogram
Specify the debug entry file addressargs
Parameters passed to the program can be found inprocess.argv
getcwd
Specifies the directory in which the program starts debuggingruntimeExecutable
Sets the runtime executable path. The default isnode
, can be other executables, such as –npm
,nodemon
runtimeArgs
Parameters passed to the runtime executable
-
Public attribute
name
:debug
The command name is displayed in the Debug drop – down windowtype
: Debug type, depending on the language,JavaScript
As a general rule, benode
request
: Debug startup type:launch
: it is todebug
Mode start debugging;attach
: is attached to the start of an already started processdebug
Mode and debugprotocol
Setting the Debugging Protocolauto
Attempts to automatically detect the protocol used by the target runtimeinspector
The new V8 debugger protocol addresses most of the legacy issues,Node versions >= 6.3 - and Electron versions >= 1.7.4.
legacy
The original V8 debugger protocol,Node versions < V8.0 and Electron versions < 1.-7.4.
port
Port used for debuggingaddress
TCP/IP address for remote debugginglocalRoot
Local address mapped during remote debuggingremoteRoot
Remote directory address for remote debuggingsourceMaps
The default is trueoutFiles
Used to specify the location of sourceMaps when the map file is not in the same directory as the JS filerestart
Automatic restart debuggingtimeout
Configure the timeout period that is automatically attachedstopOnEntry
Automatic breakpoint to the first line of codesmartStep
Automatically skips code that is not mapped to source codeskipFiles
Specifies code that skips single-step debuggingtrace
Enabling diagnostic Output
-
Launch mode properties
program
Specify the debug entry file addressargs
Parameters passed to the program can be found inprocess.argv
getcwd
Specifies the directory in which the program starts debuggingruntimeExecutable
Sets the runtime executable path. The default isnode
, can be other executables, such as –npm
,nodemon
runtimeArgs
Parameters passed to the runtime executableruntimeVersion
Sets the version of the runtime executable, which can be switched if using NVMnode.js
versionenv
Add additional environment variablesenvFile
File load environment variablesconsole
The configuration terminal can be an external terminal or an internal integration terminal. The default value is internalConsoleautoAttachChildProcesses
Tracks all subprocedures of the debug object and automatically attaches to – subprocedures that are started in debug mode
-
Attach Configuration in attach mode
processId
The specifiednodejs
processid
Since it changes every time you start, pass"${command:PickProcess}"
Platform-specific attributes
“Windows” : the Windows
“Linux” : Linux
“Osx” : macOS
Properties wrapped in Windows, Linux, and OSX will only take effect on the corresponding platform. For example:
Configurations "{"version": "0.2.0", "configurations": [{"type": "node", "request": "launch", "name": "launch Program"," Program" : "${workspaceFolder}/node_modules/gulp/bin/gulpfile.js", "args": ["myFolder/path/app.js"], "windows": { "args": ["myFolder\\path\\app.js"] }, "osx": { "stopOnEntry": false } } ] }Copy the code
launch.json
The default variable
${workspaceFolder}
: Indicates the path to open the project.${file}
: Path of the current open file.${fileBasename}
: Indicates the name of the open file, including the suffix.${fileDirname}
: Path to the folder where the open file is located.${fileExtname}
: Extension name of the current open file.${cwd}
: Current execution directory