How to run a command automatically in VS Code when you open a project

Sometimes, I trip myself up by forgetting to run Webpack when I open a JavaScript project. Usually it’s when I’m groggy in the morning and I get to a point where I expect some output to have changed, and nothing is happening. It takes me a minute to orientate myself and have that doh! moment, and realise that I haven’t spun up webpack yet. 🤦‍♂️

So, to spare myself this ignominy again, it would be great to have webpack launch when I open a JavaScript project that uses webpack.

VS Code has tasks built-in for this very thing. You can check out the Tasks User Guide for the full skinny. I will just show you to tackle my use case.

I want to execute one of my npm scripts from my package.json. From the command-line, I run npm run webpack. You can run whatever command you wish as a task.

JSON
{
"scripts":{
"webpack":"webpack --mode development --watch"
}
}

TLDR

Add the following task to the workspace tasks.json.

JSON
{
"version": "2.0.0",
"tasks": [
{
"label": "Run npm webpack on startup",
"type": "shell",
"command": "npm run webpack",
"windows": {
"command": "npm run webpack"
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"runOptions": { "runOn": "folderOpen" }
}
]
}

Enable automatic tasks yourself by running the command Tasks: Allow Automatic Tasks in Folder.

How to create a task file

The tasks specific to your project are stored in <project folder>/.vscode/tasks.json. You can create the file yourself, or you can run the Tasks: Configure Task command to build a template file for you.

The command asks you a couple of questions before creating the file.

  1. Select a task to configure: You can skip this and hit Enter.
    select a task to configure
  2. Select a task template: Select the “Others” option.
    select a task template

This is the skeleton tasks.json that you get.

JSON
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}

How to create a task

So, we want to add a new task object like “echo” above. There is Intellisense support to assist you, so you can press Ctrl+Space to get a list of the properties.

intellisense in tasks.json

Here is a list of the most important task properties:

This is what did the trick for me:

JSON
{
"version": "2.0.0",
"tasks": [
{
"label": "Run npm webpack on startup",
"type": "shell",
"command": "npm run webpack",
"windows": {
"command": "npm run webpack"
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"runOptions": { "runOn": "folderOpen" }
}
]
}

The first time you open a project that contains a task that runs on “folderOpen”, you should get a prompt asking if you want to allow tasks to run automatically in that folder. I didn’t get this prompt!

You can enable automatic tasks yourself by running the command Tasks: Allow Automatic Tasks in Folder.

automatic tasks command

The result

Next time you open your project, you will see your task running automatically like so:

demonstration of task running on startup in integrated terminal

Hurrah! One less thing to think about! 😅

Tagged