VS Code debugging
Use VS Code's built-in Node.js debugger to pause TypeScript code, inspect variables, and step through execution. tsx runs the TypeScript code and supplies source maps; you do not need to compile it first.
Choose one of these alternatives:
- Launch when you want VS Code to start the program.
- Attach when you want to start the program in a terminal yourself.
Setup
Open your project folder in VS Code and install tsx locally. These examples assume Node.js is available to VS Code and tsx is installed in that project's node_modules; check the Node.js requirements and ESM registration requirements.
Create this file in the project root:
example.ts
const name: string = 'world'
console.log(`Hello, ${name}!`)Click the gutter beside the console.log line to set a breakpoint. Then use either configuration below.
Launch from VS Code
Create this complete launch configuration:
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "tsx: current file",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--import", "tsx"],
"program": "${file}",
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**"]
}
]
}This uses the locally installed tsx loader, without relying on a global tsx executable or adding node_modules/.bin to VS Code's PATH.
Open example.ts, select tsx: current file in Run and Debug, and press F5. Execution should pause at the breakpoint. Inspect name in the Variables panel, then press F5 again to continue. The integrated terminal prints:
Hello, world!To always launch the same file instead of the active editor, replace ${file} with ${workspaceFolder}/example.ts.
Attach to a process started in a terminal
Use this alternative when you need to control the startup command yourself. Create this complete attach configuration instead:
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "tsx: attach",
"type": "node",
"request": "attach",
"port": 9229,
"skipFiles": ["<node_internals>/**"]
}
]
}From your project root, start the program with Node.js's --inspect-brk flag:
npx tsx --inspect-brk ./example.tsThe terminal prints a debugger address on port 9229, and the process waits for a debugger before running your code. Select tsx: attach in Run and Debug, then press F5. Continue past the initial pause if necessary to reach your breakpoint. After inspecting name, continue. The terminal where you started the process prints:
Hello, world!If you change the inspector port in the command, change port in the attach configuration to match. Use --inspect instead of --inspect-brk only when the program can start running before you attach.
Debugger options and troubleshooting
- To skip dependencies while stepping, add
"${workspaceFolder}/node_modules/**"toskipFiles. - If a breakpoint stays hollow or gray, run Debug: Diagnose Breakpoint Problems from the Command Palette. Confirm that the file belongs to the project you opened and that the process runs through tsx.
- For environment variables, command-line arguments, remote debugging, and other options, see VS Code's Node.js debugging reference.
