Gulp in Visual Studio

This post walks through the basics of using Gulp in Visual Studio 2013. Specifically, we will add two compile time tasks; one for linting TypeScript files, and another for linting SCSS. You are not limited to using just linters with Gulp, in fact you can do just about anything.

Gulp is specifically good at running repetitive tasks and is widely used in the non-.NET world via the command line. Other common tasks include; minification, compilation and running unit tests. Fortunately for us, some unofficial tools have been developed by Microsoft (specifically, the awesome Mads Kristensen and his dedicated team) which enable us to use Gulp directly inside Visual Studio.

Gulp is JavaScript configuration based, meaning you configure Gulp via a special file called GulpFile.js, which you add to your project.

This post specifically targets Visual Studio 2013, because at the time of writing Visual Studio 2015 hasn’t been released yet (its actually RC at this point). As Microsoft tooling is evolving rapidly at this time, i’ll avoid covering 2015 for now (however, I may revise this post after Visual Studio 2015 is released).

This post is a fork of the popular post Grunt In Visual Studio 2013.


NodeJS and npm

Gulp is built on top NodeJS and is installed via the Node Package Manager (npm). Both of these tools need to be installed to use Gulp. Check that you already have NodeJS installed, and/or download the latest version. The installation is a trivial process, so I wont document it fully here, but please ensure that the “npm package manager” feature is installed. This feature is selected by default, and it is need to install Gulp (and indeed, any other package).


Before we can use Gulp, we must install it globally.

Open a command prompt and type

npm install -g gulp

You might want to get reacquainted with running commands in this way because with the rapid state of change in the industry, development of visual tools is always going to lag behind (even with Microsoft’s new rapid development and deployment ethos, but that’s a whole other discussion).

Task Runner Explorer

This tool will run our Gulp tasks for us. It will parse our Gulp File (which we will create shortly), list our tasks, let us run our tasks individually, and let us bind our tasks to “IDE events” (pre-build, after build, clean & solution open).

To install Task Runner Explorer, go to Tools > Extensions and Updates and type Task Runner Explorer. The first result should by created by Microsoft Corp.

Task Runner Explorer

Task Runner Explorer

Local Gulp

Now that we have the necessary tooling installed, we need to add Gulp to our actual project (and any additional packages we want to use). Packages are controlled using a special file called package.json. This file contains metadata about your project, and the packages its dependent on (and potentially a bunch of other stuff that is out of the scope of this tutorial).

Create a file called package.json at the root of your project, and add the following code;

    "name": "Gulp101",
    "version": "1.0.0",
    "devDependencies": {
        "gulp": "~3.9.0",
        "gulp-scss-lint": "~0.2.1",
        "gulp-tslint": "~3.1.0-beta"

The file is pretty self explanatory, but if you want a little more information with regards to versioning, see this useful post; Node and npm Version Numbering: Guide and Best Practices.

To save time later, I have also included the other development dependencies. In this case, our TypeScript linter (gulp-tslint) and SCSS linter (gulp-scss-lint). We will configure these shortly.

To install the dependencies, open a command prompt and change the directory to that which contains your newly created package.json and run the following command;

npm install

This will result in a new directory being created, called node_modules, which will contain each dependency.


As previously mentioned, Gulp is a JavaScript based task runner. Gulp is configured/controlled using a file called Gulpfile.js. This file is responsible for defining the configuration for each task. You can have many tasks and many aliased groups that run multiple tasks.

For this tutorial we are going to validate our TypeScript and SCSS code quality using linters. Specifically, Gulp has packages called Gulp-TSLint and Gulp-SCSS-Lint (you defined and installed these packages earlier).

Add the following configuration;

var gulp = require('gulp');

var scsslint = require('gulp-scss-lint');
gulp.task('scss-lint', function () {

var tslint = require('gulp-tslint');
gulp.task('tslint', function () {
    return gulp.src('stuff/*.ts')

gulp.task('typescript', ['scss-lint', 'tslint']);

Again, the beauty of Node and Gulp especially is that its pretty simple to understand what’s going on, even for a beginner.

SCSS Lint Task

The minimum configuration required is the path to your SCSS files, using the src method. In the given example, we simply find all the files in a sub directory with the extension *.scss. You can make this more complicated, and recursive, as follows;


This will recursively find every SCSS file in all folders sitting below the Content folder.

There’s a comprehensive tutorial on how to further refine the configuration on the NPM website. The documentation on GitHub is also excellent.

TS Lint Task

Before you can lint a TypeScript file, you must define some rules that you want to enforce throughout your code-base. You can push this configuration to source control and share with your team for maximum consistency.

If you have Web Essentials installed (and I highly recommend all web developers install this excellent tool) then you can quickly generate a configuration file by clicking Web Essentials > Edit Global TSLint Settings (tslint.json). Clicking this drops a tslint.json file into your Documents folder and opens it directly in Visual Studio, where you can make edits. I highly recommend closing the file, moving it into your solution folder, and adding it as part of your project for easy access later.

If you don’t use Web Essentials are you insane? you can grab a sample configuration file directly from the GitHub source code repository and again I recommend that you add it as part of your project. I won’t go into the ins and outs of each of the TypeScript linting rules, that’s for another time. Just know that you can change them to your hearts content.

As with the SCSS linter, you specify the path of your TypeScript files using the src method. You pass in a string that represents the path of your TypeScript files.


Final Steps

Add some code

There is just one more step we need to take before running our tasks, we need some code to test!

Add a new SCSS file to your project, and a new TypeScript file. Add the following beautifully written ugly code to the TypeScript file;

module SomeModule {

    export class SomeClass {

        str: string;

        constructor() {
            this["str"] = "Hello, World"

The main issues with this code are trailing whitespace, no "use strict", missing semi-colons and access to a property via a string literal. We will let Gulp tell us about these shortly.

Open the task runner

Open the task runner by going to View > Other Windows > Task Runner Explorer.

Select your project (if it is not selected by default) and click the refresh button. Your Gulpfile.js configuration file should be loaded and the alias tasks and individual tasks should be displayed.

You can manually run a task (or set of tasks) by right clicking and selecting Run. If all went well, you should see the following output;

c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp> cmd.exe /c gulp -b "c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp" --color --gulpfile "c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp\gulpfile.js" typescript
[13:48:21] Using gulpfile c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp\gulpfile.js
[13:48:21] Starting 'scss-lint'...
[13:48:21] Finished 'scss-lint' after 5.33 ms
[13:48:21] Starting 'tslint'...
[13:48:21] [gulp-tslint] error (no-string-literal) helloworld.ts[8, 18]: object access via string literals is disallowed
[13:48:21] [gulp-tslint] error (semicolon) helloworld.ts[8, 41]: missing semicolon
[13:48:21] [gulp-tslint] error (use-strict) helloworld.ts[1, 1]: missing 'use strict'
[13:48:21] 'tslint' errored after 68 ms
[13:48:21] Error in plugin 'gulp-tslint'
    Failed to lint: helloworld.ts[8, 18]: object access via string literals is disallowed, helloworld.ts[8, 41]: missing semicolon, helloworld.ts[1, 1]: missing 'use strict'.
Process terminated with code 1.

Success! Wait what? Oh yes, the task has failed because our TypeScript file has lots of linting errors. Open up the TypeScript file and use the output message to fix each problem. Once done re-run the task again by right clicking and clicking run. Once all issues are resolved, you should get the following;

c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp> cmd.exe /c gulp -b "c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp" --color --gulpfile "c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp\gulpfile.js" typescript
[13:49:12] Using gulpfile c:\Users\jon.preece\Documents\Visual Studio 2013\Projects\gulp\gulp\gulpfile.js
[13:49:12] Starting 'scss-lint'...
[13:49:12] Finished 'scss-lint' after 5.36 ms
[13:49:12] Starting 'tslint'...
[13:49:12] Finished 'tslint' after 64 ms
[13:49:12] Starting 'typescript'...
[13:49:12] Finished 'typescript' after 7.33 μs
Process terminated with code 0.


Right clicking and clicking run is a little too manual for my liking, this is an automation tool isn’t it? Alas, the Task Runner Explorer provides you with Bindings. Basically you can hook in to events in the IDE and run your tasks automatically when one of events happens.

Available bindings;

  • Before Build
  • After Build
  • Clean
  • Solution Open

To bind a task to a binding, right click the task (or again, the aliased task) and select Bindings > {An IDE Event of your choice}. In my case, I have chosen Before Build. So when I press Ctrl+Shift+B the tasks are ran automatically.

Note that as far as I know, you can’t fail the build if a task fails at this point. No doubt this is an upcoming feature.


Gulp is a brave new world for most .NET developers. Tooling is certainly in its infancy (within Visual Studio) at this point, but the awesome power of NodeJS, npm and Gulp can be harnessed with some useful extensions.