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.
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.
{
"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"
}
]
}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.
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.
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
22:40
24:16
9:48
11:32
12:00
18:00