Chronicling the Adventure that is software Development

Package Managers

Many of us .NET developers have spent the majority of our lives living in the cozy NuGet Package Manager bubble. With the release of Visual Studio 2015 and NET5 we, Microsoft Devs, are introduced to 2 new package managers that really show how the love is shifting to the client side. Don’t worry NuGet fans, these new package mangers wont replace Nuget, instead they will provided exciting new support to the font end of our web applications.

Now, each of these package managers are designed with specific needs in mind and they each handle specific tasks better than the others. Because of their different strengths, it is common to use all three of them within the same .Net web application.

NuGet

What is it? .Net developers are likely to be very familiar with this. It is a package manager that mainly deals with .NET assemblies.

What it is good for?It is nicely integrated within Visual Studio and great for loading .Net assemblies and libraries such as Entity Framework and ASP.NET Identity.

NPM (Node Package Manager)

What is it? Designed specifically for node modules, but is also ideal for loading packages that are used during development time. Unlike Bower, NPM supports nested dependency trees. Meaning, NPM may actually load multiple versions of a component on your machine.

What it is good for?Great for managing developer tools like Yeoman, Grunt, Gulp, and testing tools. Its nested dependency tree makes it great for avoiding dependency conflicts.

Bower

What is it?Bower is optimized for front-end components. Bower uses a flat dependency tree, requiring only one version for each package, reducing page load to a minimum. So where NPM aims for stability, bower aims for a smaller/faster footprint.

What it is good for?Created specifically for managing front-end components like javascript and css files.

What did we just do?

In our package.json file, we’ve indicated that we need three NPM packages named grunt, grunt-contrib-uglify, and grunt-contrib-watch. After the name of each package, we’ve specified the version of the package that we need. Grunt and Bower are now installed in our project!

Add a Grunt Configuration File

1. Right click the project and select Add > New Item. Select Grunt Configuration File and name it gruntfile.js

What is Grunt?

Grunt is an open-source tool that you can use to build all of the frontend resources for your project. For example, you can use Grunt to compile your Less or Sass files into CSS. You can also use Grunt to combine and minify CSS and JavaScript files.

2. Open up gruntfile.js and load your plugins by adding the following.

What did we just do?

We told Grunt to look for a javascript file named app.js and then, on build, we want it to minify (uglify) the app.js file and put it into our wwwroot (we will create the app.js file later in this tutorial).

Add JQuery and Bootstrap to our project using Bower

What is Bower?

Bower is a package manager created by Twitter that is designed to support front-end development. You can use Bower to manage resources such as AngularJS, jQuery, and Bootstrap.

1. Right click the project and select Add > New Item. Select Bower Configuration File and name it bower.json

2. Open up bower.json and add JQuery and Bootstrap to its dependencies

3. Your bower.json file and projectfolder structure should now look like this:

Wow, now we have Nuget, NPM, and Bower package managers in our project. Why do we need all of them and what do they do? Check out my blog post about that here – Nuget vs NPM vs Bower.

Installing AngularJS

1. Open up bower.json and add these AngularJS modules to its dependencies. For this project we will just be using the “angular package”, but I wanted to show here that “angular-resource” and “angular-route” are added in the same way.

2. Save and compile the project and you will see that the above modules were added to you wwwroot folder and your Bower folder. Should look like this:

Add Scripts

1. Right click the project and select Add > New Folder. Name it “Scripts”.

2. Right click the Scripts folder and select Add > New Item… Select JavaScript File and name is app.js

3. This is the same app.js file that we look for in our gruntfile.js code a few steps above. The next thing we will do is initialize the angular application. To do this, add the following to app.js

var app = angular.module("myApp", []);

4. To have Grunt uglify app.js and add it to our wwwroot folter, do the following. Open up Visual Studio’s Task Runner Explorer (View > Other Windows > Task Runner Explorer)

5. Refresh the Task Runner Explorer. You should see your Gruntfile.js there. Drill down into Alias Tasks. Right click and run default. This will minify (uglify) your app.js file and add it to your wwwroot.

Use static files

1. You want to tell your app to use static files like app.js and the other angular and bootstrap files. To do this open up Startup.cs and add this to the Configure(IApplicationBuilder app) method.

app.UseStaticFiles();

2. The method Configure(IApplicationBuilder app) should now look like this:

What did we just do?

In the above html we are loading Bootstrap, JQuery, AngularJS, and app.js. We are then adding ng-app=”myApp” to the HTML tag. The ng-app directive defines an AngularJS application and its module is created in app.js. If these angular terms are unfamiliar to you, please check out http://www.w3schools.com/angular/angular_intro.asp for a more detailed explanation of the Angular code seen here.