Purpose of launch.json in VS Code Debugging
In Visual Studio Code, the launch.json file is the
central configuration file used to set up and customize debugging
sessions. This article explains the purpose of this file, how it works,
and how developers use it to configure runtimes, arguments, and
environment variables to successfully debug their applications.
What is the launch.json File?
The launch.json file is a JSON-formatted configuration
file located inside the hidden .vscode directory at the
root of a project workspace. Its primary purpose is to store debugging
configurations, allowing developers to start, attach to, and control
their application debugging processes directly from within VS Code.
Instead of typing complex command-line arguments every time you want
to debug, the launch.json file saves these settings so you
can launch your debugger with a single click or keyboard shortcut.
Key Functions of launch.json
The launch.json file serves several critical roles in
the development workflow:
- Defines the Debugger Type: It specifies which debugging extension to use (such as Node.js, Python, C++, or Go) based on the language of your project.
- Controls the Debugging Mode: It determines whether
VS Code should launch a new instance of your application
(
launchmode) or connect to an already running process (attachmode). - Passes Arguments and Environment Variables: You can
use it to inject specific command-line arguments or environment
variables (like
PORTorNODE_ENV) directly into your application at runtime. - Automates Pre-Launch Tasks: It can trigger build
tools or compilers (such as compiling TypeScript to JavaScript) before
the debugging session starts using the
preLaunchTaskproperty.
Core Properties Inside launch.json
A standard launch.json file contains a list of
configurations, each containing several key-value pairs. The most common
properties include:
type: The type of debugger to use (e.g.,node,python,cppdbg).request: The request type, which is eitherlaunch(starts the app in debug mode) orattach(connects to an already running app).name: A reader-friendly name for the configuration that appears in the VS Code debug dropdown menu.program: The path to the executable or startup file that the debugger should run.args: An array of command-line arguments passed to the program during launch.env: A dictionary of environment variables defined specifically for the debugging session.
By customizing these properties, developers can create multiple configurations for different environments, such as “Debug Unit Tests,” “Launch Server,” or “Attach to Remote Container,” streamlining the entire development lifecycle.