VS Code SetupIntermediate

How to Run Build and Dev Commands with tasks.json in VS Code

Define reusable tasks so you can start your build, dev server, or test command from a menu or keyboard shortcut instead of retyping it.

8 minIntermediate

If you run the same handful of commands all day (build, dev, test, lint) you can save them as VS Code tasks. A task is a named shortcut to a shell command, runnable from a menu, the Command Palette, or a keystroke. This guide defines a few tasks in tasks.json and runs them, so you stop retyping npm commands.

What you need

  • A project with npm scripts (a package.json with a scripts section)
  • VS Code open on that project
  • A few minutes

Step 1: Open the tasks configuration

Open the Command Palette with Ctrl+Shift+P, type Tasks: Configure Task, and press Enter. Choose Create tasks.json file from template, then Others. VS Code creates a .vscode/tasks.json file you can edit.

Step 2: Define your tasks

Replace the contents with tasks that run your npm scripts. Each task has a label you will see in the menu, a type of shell, and the command to run. Mark the build task as the default build task so it runs with one shortcut.

.vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "dev server",
      "type": "shell",
      "command": "npm run dev",
      "isBackground": true,
      "problemMatcher": []
    },
    {
      "label": "build",
      "type": "shell",
      "command": "npm run build",
      "group": { "kind": "build", "isDefault": true }
    },
    {
      "label": "test",
      "type": "shell",
      "command": "npm test"
    }
  ]
}
Why isBackground matters
A dev server never exits, so mark it isBackground: true. Without that flag VS Code waits for the command to finish and the task appears to hang.

Step 3: Run a task from the menu

Open the Command Palette, type Tasks: Run Task, and press Enter. Your three tasks appear in a list. Pick one and it runs in a terminal panel. The output streams just like a normal terminal command.

VS Code - Run Task
Tasks: Run Task
------------------------------------
dev server
build
test
The task picker listing your defined tasks.

Step 4: Run the build with a shortcut

Because you marked build as the default build task, you can run it without the menu. Press Ctrl+Shift+B (Cmd+Shift+B on macOS) and the build runs immediately.

VS Code task - build
Executing task: npm run build
vite build
dist/index.html 0.42 kB
built in 1.84s
$

Result

Your common commands are now named tasks you can launch from a menu or a single keystroke, and the build runs with one shortcut. No more typing the same npm command twenty times a day.

Watch related tutorials

Tags
#tasks#tasks-json#build#automation#terminal