154

I am trying to install a dependency with Bower using a URL. As of Bower documentation:

Bower offers several ways to install packages:

    # Using the dependencies listed in the current directory's bower.json 
    bower install
    # Using a local or remote package 
    bower install <package>
    # Using a specific version of a package 
    bower install <package>#<version>
    # Using a different name and a specific version of a package 
    bower install <name>=<package>#<version> 

Where <package> can be any one of the following:

  • A name that maps to a package registered with Bower, e.g, jquery.
  • A remote Git endpoint, e.g., git://github.com/someone/some-package.git. Can be public or private.
  • A local endpoint, i.e., a folder that's a Git repository.
  • A shorthand endpoint, e.g., someone/some-package (defaults to GitHub).
  • A URL to a file, including zip and tar files. Its contents will be extracted.

However, then it says, that all the types except the URL allow to specify a version.

How do I specify a version for a URL downloaded dependency?

Shashank Agrawal
  • 21,660
  • 9
  • 71
  • 105
Edmondo1984
  • 17,841
  • 12
  • 55
  • 99

10 Answers10

194

Use a git endpoint instead of a package name:

bower install https://github.com/jquery/jquery.git#2.0.3
Sindre Sorhus
  • 62,754
  • 35
  • 155
  • 217
  • 8
    This is a Git endpoint, and specifying the versioning works. If you specify for example a Javascript file directly, this does not work – Edmondo1984 Oct 14 '13 at 04:51
  • 1
    URLs are allowed with recent versions of bower. – lfender6445 Aug 28 '14 at 00:48
  • BTW: works with `svn` too, e.g. `bower install crypto-js=svn+http://crypto-js.googlecode.com/svn/#~3.1.2 --save` – Preexo Oct 27 '14 at 09:29
  • 5
    if you have an error `ENORESTARGET URL sources can't resolve targets` when trying install from git with a committish, you have to change `https://github.com/jquery/jquery` to `https://github.com/jquery/jquery.git` (add `.git`) – jakub.g Feb 03 '16 at 16:37
  • 2
    Does bower normalise the version tag to prefix it with a `v`? When I do `bower install https://github.com/my/repo.git#1.0.0` it works even though the actual tag I pushed was called `v1.0.0`. – spinningarrow Feb 24 '16 at 04:04
  • Is it possible to define the specific directory for bower to install, within the git repo to grab, rather than grabbing everything inside it? – wharfdale Jan 18 '17 at 18:01
55

If you use bower.json file to specify your dependencies:

{
     "dependencies": {
         ...
         "photo-swipe": "git@github.com:dimsemenov/PhotoSwipe.git#v3.0.x",

#bower 1.4 (tested with that version) can read repositorios with uri format

         "photo-swipe": "git://github.com/dimsemenov/PhotoSwipe.git#v3.0.x",

     }
}

Just remember bower also searches for released versions and tags so you can point to almost everything, and can interprate basic query patterns like previous example. that will fetch latest minor update of version 3.0 (tested from bower 1.3.5)

Update, as the question description also mention using only a URL and no mention of a github repository.

Another example is to execute this command using the desired url, like:

bower install gmap3MarkerWithLabel=http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.0/src/markerwithlabel.js -S

that command downloads your js library puts in {your destination path}/gmap3MarkerWithLabel/index.js and automatically creates an entry in your bower.json file called gmap3MarkerWithLabel: "..." After that, you can only execute bower update gmap3MarkerWithLabel if needed.

Funny thing if you do the process backwars (add manually the entry in bower.json, an then bower install entryName) it doesn't work, you get a

bower ENOTFOUND Package gmapV3MarkerWithLabel not found

le0diaz
  • 2,298
  • 21
  • 28
  • ++ RE caveat about doing the process backwards – Jakob Jingleheimer Nov 18 '15 at 00:46
  • As of 2018. People should move away from bower, not in development anymore, and use any other package management tool like Yarn. We've had issues as many 3rd party library developers stopped releasing bower packages to go with a better standarized way. Yarn is way faster than bower and really easy to migrate. – le0diaz Apr 19 '18 at 13:53
22

Targeting a specific commit

Remote (github)

When using github, note that you can also target a specific commit (for example, of a fork you've made and updated) by appending its commit hash to the end of its clone url. For example:

"dependencies": {
  "example": "https://github.com/owner_name/repo_name.git#9203e6166b343d7d8b3bb638775b41fe5de3524c"
}

Locally (filesystem)

Or you can target a git commit in your local file system if you use your project's .git directory, like so (on Windows; note the forward slashes):

"dependencies": {
  "example": "file://C:/Projects/my-project/.git#9203e6166b343d7d8b3bb638775b41fe5de3524c"
}

This is one way of testing library code you've committed locally but not yet pushed to the repo.

JcT
  • 3,429
  • 1
  • 23
  • 32
  • 1
    Thanks! I was using a forked bower component with a patch, and it kept installing the latest according to the main bower file for that repo. Adding the commit hash allowed me to download my most recent version. – elliottregan Jun 21 '17 at 00:15
  • Can we do this with branch name? – Roel Oct 22 '20 at 13:19
21

Use the following:

bower install --save git://github.com/USER/REPOS_NAME.git

More here: http://bower.io/#getting-started

Shashank Agrawal
  • 21,660
  • 9
  • 71
  • 105
9

Just an update.

Now if it's a github repository then using just a github shorthand is enough if you do not mind the version of course.

GitHub shorthand

$ bower install desandro/masonry
Turdaliev Nursultan
  • 2,350
  • 1
  • 18
  • 26
7

Just specifying the uri endpoint worked for me, bower 1.3.9

  "dependencies": {
    "jquery.cookie": "latest",
    "everestjs": "http://www.everestjs.net/static/st.v2.js"
  }

Running bower install, I received following output:

bower new           version for http://www.everestjs.net/static/st.v2.js#*
bower resolve       http://www.everestjs.net/static/st.v2.js#*
bower download      http://www.everestjs.net/static/st.v2.js

You could also try updating bower

  • npm update -g bower

According to documentation: the following types of urls are supported:

http://example.com/script.js
http://example.com/style.css
http://example.com/package.zip (contents will be extracted)
http://example.com/package.tar (contents will be extracted)
lfender6445
  • 25,940
  • 9
  • 95
  • 82
7

Here's a handy short-hand way to install a specific tag or commit from GitHub via bower.json.

{
  "dependencies": {
    "your-library-name": "<GITHUB-USERNAME>/<REPOSITORY-NAME>#<TAG-OR-COMMIT>"
  }
}

For example:

{
  "dependencies": {
    "custom-jquery": "jquery/jquery#2.0.3"
  }
}
F Lekschas
  • 11,503
  • 8
  • 51
  • 65
  • Just out of curiosity, is it possible to target a specific directory from the repository? git@git-url.git#v0.1.0/directory ? – Rhys Jun 22 '17 at 01:21
3

I believe that specifying version works only for git-endpoints. And not for folder/zip ones. As when you point bower to a js-file/folder/zip you already specified package and version (except for js indeed). Because a package has bower.json with version in it. Specifying a version in 'bower install' makes sense when you're pointing bower to a repository which can have many versions of a package. It can be only git I think.

Shrike
  • 8,358
  • 7
  • 61
  • 98
3

Try bower install git://github.com/urin/jquery.balloon.js.git#1.0.3 --save where 1.0.3 is the tag number which you can get by reading tag under releases. Also for URL replace by git:// in order for system to connect.

SACn
  • 1,655
  • 1
  • 9
  • 26
0

Installs package from git and save to your bower.json dependency block.

  1. bower register package-name git-endpoint#version
  2. install package-name --save

(--save will save the package name version in the bower.json file inside the dependency block).

Reference

Alexei - check Codidact
  • 17,850
  • 12
  • 118
  • 126