visual-studio-codenpm

How can I write a VS Code Task to run my NPM script?


I want to add a task to VS Code so that by running it, I will be able to run 'npm run dev' command. what should I add to my task.json file? currently it's like this:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "msbuild",
      "args": [
        "/property:GenerateFullPaths=true",
        "/t:build",
        "/consoleloggerparameters:NoSummary"
      ],
      "group": "build",
      "presentation": {
        "reveal": "silent"
      },
      "problemMatcher": "$msCompile"
    },
    {
      "type": "npm",
      "script": "dev",
      "path": "client",
      "problemMatcher": [],
      "label": "npm: dev - client",
      "detail": "vite"
    },
    {
      "type": "npm",
      "script": "install",
      "path": "client",
      "group": "clean",
      "problemMatcher": [],
      "label": "npm: install - client",
      "detail": "install dependencies from package"
    }
  ]
}


Solution

  • For many cases, you might not even need to write a task for this manually. VS Code has a feature called "Task Auto-Detection", which includes automatically adding tasks for NPM scripts in your package.json file.

    Just open the menu to select a task to run using Tasks: Run Task in the command palette, open the "npm" sub-menu, and then select your script from that sub-menu.

    If you want to debug your script instead of just running it, use the Debug: Debug npm script command.