This is a simple VSCode launch configuration for debugging TypeScript code that will work with most TypeScript projects.
It allows you to easily debug your project using the awesome debugging experience in VSCode.
It works perfectly for library and CLI projects that are written in TypeScript and are targeting CommonJS.
-
Create a
.vscode/
folder in your workspace at the top-level, if you don't have that folder already. -
Add the configuration below to
.vscode/launch.json
. -
Before using the configuration below, make sure you update the
cwd
field to point to the exact folder that holds your TypeScript project, if you are using a workspace that has multiple subfolders.For example, if your
tsconfig.json
file is in${workspaceFolder}/my-typescript-project/
, make sure to set that as the value ofcwd
, like:"cwd": "${workspaceFolder}/my-typescript-project/"
. -
Set any arguments that your library or CLI expects in the
args
field, or leave it empty. -
The default mode for debugging is
transpile-only
, which ignores TypeScript errors and lets the project start up right away. If you don't want to ignore errors, changets-node/register/transpile-only
tots-node/register
. -
Click the "Run and Debug" button in the sidebar, then click the green play button (making sure the launch configuration below is selected in the dropdown). That's it!
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}/",
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only"
],
"args": []
}
]
}
I've also included a sample tsconfig.json
file that you can use to get started.
This has been tested with the configuration above, running on node v18
.
{
"compilerOptions": {
"rootDir": "src/",
"outDir": "dist/",
"moduleResolution": "node",
"module": "CommonJS",
"esModuleInterop": true,
"lib": [ "ES2021" ],
"target": "ES5",
"strict": true
}
}