Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Package Management

For those who have a .NET background, package management should already be familiar. If you are comfortable with the concept of package management, feel free to skip the next paragraph, although you might learn a thing or two even if you are already comfortable.

There is a saying that if you don’t manage your packages, your packages will manage you. (In my experience, this is certainly true.) In the extremely innovative front-end community, libraries are constantly being updated, and many of these libraries rely on other libraries, which in turn rely on other libraries. This can cause a mess of dependencies that either forces you to spend hours resolving these dependencies or ultimately forces you to give up. This is where package managers come in. They maintain a list of libraries and their dependencies, and will help you automatically resolve these dependencies.

The first package manager discussed here is NPM.

NPM

The first thing you should know about NPM (Node Package Manager) is that’s automatically installed when you install Node. So, take the next 10 seconds to celebrate not having to install anything for this topic.

NPM is by far one of the most common tools for web developers. Where Node allows developers to build great development tools, NPM allows the developers to share their libraries, while simultaneously making it easy for people to install and update. NPM is by far the largest package manager for JavaScript libraries, boasting more than 125,000 packages at the time of this writing, with over a billion installs (that’s right, a billion, with a B) per month.

NPM can be accessed via command line. So, if you are on a Windows PC, open the command prompt. If you’re running OS X or Linux, open Terminal and type npm.

If you are experiencing errors, see the information about Node earlier (what it is and how to install it.).

Now that you have NPM up and running, let’s install a package.

If you closed the command prompt or Terminal, open it again and run the following command:

npm install -g bower

Let’s break this command down so that you better understand what it’s doing. By typing npm install, you are invoking NPM and telling it to install a package called Bower. The -g flag is telling Node to install this package globally. The reason you install Bower globally is that it is not project dependent.

If you receive an error message when trying to install Bower, you may need to run the command with elevated privileges. On a Windows computer, try running the command prompt as an administrator. If you are on OS X or Linux, try running the command with the sudo modifier:

sudo npm install -g bower

That should prompt you for your password, and even though it doesn’t look like you are typing anything, you are.

Follow these tips if you run into any problems installing packages throughout the remainder of this hour.

Bower

Bower is a package manager. Wait, didn’t you just learn that was what NPM is? That’s right! At this point, you might be thinking that the web development community just likes to complicate things (or that somebody is pulling your leg), and you would be partially right.

Bower is indeed another package manager, but one that focuses on installing front-end frameworks, libraries, and assets. While NPM can indeed install all of these things, it does so in a much different way. The following sidebar covers the differences between NPM and Bower.

To get started with Bower, create a new folder somewhere easily accessible, and navigate to it using your operating system’s terminal. Bower works just like NPM, where all you have to do is type bower install, but it can also grab files from other places. Here are some examples of ways to install libraries, assets, or even git repositories.

#### registered package
$ bower install jquery
#### GitHub shorthand
$ bower install desandro/masonry
#### Git endpoint
$ bower install git://github.com/user/package.git
#### URL
$ bower install http://example.com/script.js

Bower packages are installed in a subfolder called bower_components inside whatever folder you are currently in when you run the command.

Grunt

Grunt, at its core, is a task runner. Add a few of the hundreds of community-made plug-ins and you can automate your entire build process with just a few lines of code. Grunt uses Node’s require() system, along with adding some things to the package.json file. So, make sure to install Grunt as follows:

sudo npm install -g grunt-cli

This installs the Grunt command-line interface (CLI) globally so that it’s available to use in any of your projects. After you have it installed, you can start using it in your projects. To set it up, first make sure that you have a package.json file in your project directory. If you don’t, you can easily create one by answering a few questions after running the following command:

npm init

After answering questions from Node, you need to generate a Gruntfile, which is where you can set up tasks. To do that, run the following command:

npm install grunt --save-dev

The --save-dev flag tells the Grunt CLI that you want to make Grunt a development dependency.

This allows your fellow teammates, with just a single command, to make sure that they have all the necessary Node packages installed:

npm install

To add other premade Grunt tasks to your project, simply install them the same way you did Grunt. Here is the command to install JSHint, which is a code-validation task that ensures you write both valid and good JavaScript code:

npm install grunt-contrib-jshint --save-dev

Now that you have JSHint installed, let’s take a look at your Gruntfile. It should look something like this:

module.exports = function(grunt) {

  grunt.initConfig({
  });

};

To configure JSHint and set up a task to run it, modify your Gruntfile like so:

module.exports = function(grunt) {

  grunt.initConfig({
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        globals: {
          jQuery: true
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');

  grunt.registerTask('default', ['jshint']);

};

This sets some options for JSHint, like what files it should examine and what globals you will have defined. Grunt then needs to make sure it loads the tasks, and finally, you register a task called default that will run JSHint. To actually run JSHint, just run the following:

grunt

This executes the default task that you just set up to run JSHint! While your IDE of choice may come with some built-in tools for front-end development, thousands of plug-ins can be used in any combination, providing you with the flexibility to automate your entire build. Grunt is an amazingly powerful task runner that can save you loads of time with your build and deployment process.

Yeoman

After reading through this hour, you might be thinking that it takes a lot of work to get all these tools set up for every new project you work on, and you wouldn’t be wrong. This is where Yeoman comes in. Yeoman is a scaffolding tool that allows anyone to write plug-ins for it. It’s simple to use and infinitely extensible. If you find yourself creating the same config files, installing the same packages, and scaffolding out the same folder structure every time you start a new project, you’re going to love Yeoman. If this is your first foray into JavaScript programming, you could probably use a little jumpstart. Let’s use Yeoman to scaffold out a brand new Angular application. To do so, let’s first install Yeoman, as follows:

npm install -g yo

Just like when you use -g in the Grunt install command, this ensures that Yeoman is installed globally so that you can use it wherever you are.

Let’s also run the following command to install the Yeoman Angular generator globally:

npm install -g generator-angular

At this point, make a new project folder and cd into it from your command prompt. Then run the following command:

yo angular MySweetNewAppName

Yeoman is going to ask you a few questions to make sure that it installs all the features you want. Once you complete all the prompts, Yeoman installs all the dependencies it needs and scaffolds a simple Angular application for you. To view what’s created, run the following Grunt command:

grunt serve

This command starts the Angular application within a locally running Node server where you can play with the app and debug it.

  • + Share This
  • 🔖 Save To Your Account