Create Node Task for Visual Studio Code

Create Node Task for Visual Studio Code

I love the new Visual Studio Code editor/IDE. It’s fast, simple, lightweight, and an overall pleasure to work with. I love being able to just “open a folder” and work with files directly – all while getting great intellisense.

One feature that is particular interesting to me is the ability to run from the IDE. These tasks can be anything from running gulp commands, to MSBuild, to most anything you can think of. Given that JavaScript – and Node in particular – is one of the sweet spots, I assumed that there would be a built-in task to run a file with Node. After all, there is. But one thing I often like to do is to simple run a single file with node from the command line:

> node index.js

So I don’t need debugging. I want to execute Node against whatever file I have opened at the moment. It turns out this was not built-in – but the good news is that it was not difficult to add the functionality.

First, Ctrl+Shift+P to bring up the Command Palette. Then you want to type “Configure Task Runner”. That will open a “tasks.json” file which it will create in a local “.settings” directory. Inside there is a default TypeScript task along with a bunch of commented out tasks to show example of running on commands like gulp, MSBuild, etc. (and yes, just pretend that JSON actually has comments). You can remove everything in this file and just add:

{ "version": "0.1.0", "command": "node", "isShellCommand": true, "args": ["${file}"] }

One thing to notice is the this file allows tokens such as ${file} to represent the current opened file. Now Save the “tasks.json” file (if you don’t already have Auto-Save turned on) and return back to any of your JavaScript files. Now you can just bring up the Command Palette again and type “Run Task” – hit ENTER and “node” (which is what we named our task) will be the only thing in the list. Run that and the Output window will appear in split screen the the resulting output of your node file.

!

Yes, of course, I can just go over to a console window and type: “node index.js” as I’ve always traditionally done. But this makes it even easier/faster and let’s me stay within my IDE. Plus, this will always work against whatever file I happen to have opened at the time.

One bizarre thing I’ve found is that I don’t (yet) see any way to add multiple tasks to the “tasks.json” file - there does seem to be a way to add multiple tasks for a single command, but not multiple tasks for different commands. Hopefully, this will be a feature that is addressed in new versions of VS Code.

posted on Wednesday, May 20, 2015 3:18 PM Print

This article is part of the GWB Archives. Original Author: Steve Michelotti

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.