Wiring Gulp Into Project Startup

VS Code Tasks can run command-line tasks on demand by name, but they can also be configured to fire when a project opens. For a workflow that begins with gulp every time, that autostart behavior is the useful part. The basic task definition is enough to launch Gulp:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run Gulp",
      "command": "gulp",
      "type": "shell",
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

Breaking on Node Versions

That setup eventually stopped working on a machine using nvm for Node version management. Even after aliasing the default to a Node release that works with Gulp, the task still saw the wrong Node version, so gulp failed. The fix is to run nvm use first: it picks the version from the project's .nvmrc, after which Gulp runs cleanly.

A fresh terminal window handled this fine. The task, however, did not — even when the two commands were chained with a semicolon:

"command": "nvm use; gulp",

The task environment reported no knowledge of nvm at all. The root cause of the discrepancy between terminals was never pinned down, but the practical detail is that the global nvm installation includes a shell script whose sole job is defining the nvm command. Sourcing that script makes nvm available.

The Working Task Configuration

Sourcing the nvm script before invoking nvm use and then gulp produces the final setup:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run Gulp",
      "command": ". ~/.nvm/nvm.sh; nvm use; gulp",
      "type": "shell",
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

With this in place, Gulp starts automatically whenever the project opens.