Using Bower

Bower js

To be honest, my first experience with Bower was bad. I though "I prefer download and unzip the file" ... very wrong! Bower can help us managing our packages, for example install Bootstrap, jQuery, Backbone, etc. Also it can upgrade libraries and other things that will see below.

Installing bower
To install Bower we need some things before start with it:

  • Node and npm: Because Bower is a Node module.
  • Git: Bower packages are Git repos, so we need Git.
  • Global installation: It's optional, you can install Bower in every project.

To install Bower:

npm install -g bower

Using bower
If you want to install jQuery, just run the following command:

bower install jquery

After that, a new bower_components folder has appeared.

bower_components/
---jquery/

If you want to check that the asset you want is a Bower package you can do a search:

bower search <package-name>

Another commands:

List your packages:

bower list

Check updates:

bower check-new

Upgrade package:

bower update <package-name>

Get information about any package you installed

bower info <package-name>

Advanced configuration

I don't like the bower_components folder, you can change it via .bowerrc file.
Create a .bowerrc with this content:

{
  "directory": "src/packages"
}

Here is an example structure:

.bowerrc
src/
--packages/
gulpfile.coffee
package.json
bower.json
.git
.gitignore

In the structure above, you can see a bower.json file. It's very similar to node's package.json file, which specify some options.

If you type bower init you can see a bower.json like that (very similar):

{
  "name": "bower-example-mario-araque",
  "version": "0.0.1",
  "authors": [
    "author <author@author.com>"
  ],
  "description": "An example project using bower.",
  "main": "index.html",
  "license": "MIT",
  "private": true,
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "devDependencies": {
    "jquery": "~2.1.1"
  }
}

See de devDependencies object at the bottom. That object will be updated automatically if you append --save-dev in a bower install.

bower install jquery --save-dev

It provides sync with the whole team because if another member run bower install, it installs all the packages in the devDependencies object. Very useful I think.