How to Add a New Task in VS Code

A guide on how to add and configure custom tasks in VS Code for your projects.

When you’re building a VS Code extension, or really any project, you’ll often have commands you run repeatedly. VS Code tasks make it easier to run these commands directly from the editor. This is my personal note on how I set up tasks for my projects.

Here’s how to add a new task in VS Code:

  1. Open the Command Palette: Press Cmd+Shift+P (macOS) or Ctrl+Shift+P (Windows/Linux).
  2. Type “Tasks”: Start typing “Tasks” and select “Tasks: Configure Task”.
  3. Choose a Task Type:
    • If you already have a tasks.json file, VS Code will ask you to select a task to configure.
    • If you don’t have one, it will prompt you to “Create tasks.json file from template”. Choose “Others” for a blank template, or select a language-specific template if it fits your project.
  4. Edit tasks.json: VS Code will open (or create) your .vscode/tasks.json file. This is where you’ll define your custom tasks.

Here’s an example of a .vscode/tasks.json file I use for a VS Code extension project, with explanations for each field:

JSON
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "pnpm: watch",
            "type": "shell",
            "command": "pnpm",
            "args": ["run", "watch"],
            "isBackground": true,
            "problemMatcher": "$tsc-watch",
            "presentation": {
                "reveal": "never"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "pnpm: build",
            "type": "shell",
            "command": "pnpm",
            "args": ["run", "build"],
            "problemMatcher": "$tsc",
            "group": "build"
        }
    ]
}

Let’s break down what each of these fields means and why I use them:

Inside each task object:

By setting up tasks like this, I can quickly build and watch my projects without leaving VS Code, making my workflow much smoother.