Create Visual Studio Code Task for Running Node.js Scripts

Shahriar Shovon

Supported OS

  • All

Categories

Share this post with your friends

In this post, I will show you how to create a tasks.json file for Visual Studio Code for running Node.js scripts.

Requirements

I assume you have Node.js installed on your computer and it is in the PATH of your operating system. I also assume you have Visual Studio Code installed on your computer.

Creating tasks.json File

First create a tasks.json file in your .vscode/ directory in your Visual Studio Code project root. Now copy and paste the following codes to tasks.json file: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Run with Node.js", "type": "shell", "command": "node", "args": [ "${fileBasename}" ], "problemMatcher": [] } ] } Now let's explain what I did in tasks.json file. tasks.json is a JSON file. Here label is what you see when you go to Tasks -> Run Task... type should be shell, because we are running node command in a shell and showing the output. command is the command that should run when you click on Tasks -> Run Task... and select your task. args is an array of arguments that should be passed to the command ${fileBasename} is a Visual Studio Code's tasks.json variable. That is replaced by the currently used file name.

Running Node.js Script Using Tasks

Now go to Tasks -> Run Task... and select your Task. Your currently opened Node.js script should run and you should see the output. Thanks for reading this article.