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 Tasks 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 native support for debugging Node code. 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.