Bower: Install 2 versions of jQuery
Asked Answered
G

4

76

How would I go about installing 2 versions of jQuery using bower? I want to have v2.0 as well as 1.9.1 for browser support fallback

The issue I'm having is that if you run bower install jquery#1.9.1 jquery#2.0.0 the first version gets overwritten by the second because they are the same component

Gundry answered 8/5, 2013 at 13:28 Comment(3)
Hope this can help you,forum.jquery.com/topic/…Orsa
@Orsa i dont need two versions running at the same time, I want to install two seperate versions using bowerGundry
editing tags, since this isn't really a jQuery question.Luting
L
67

In the dependencies part of your bower.json you can have something like this:

"dependencies": {
    "jquery": "2.0.0",
    "jquery-1.9.1": "http://code.jquery.com/jquery-1.9.1.js"
}

One shouldn't normally have to do this, but sometimes you have to maintain / migrate an existing website that (for whatever reason) uses different versions of jquery in different pages!

Lifeblood answered 15/5, 2013 at 20:17 Comment(2)
what about: "jquery-1.9.1": "jquery#1.9.1"Magnifico
"dependencies": { "jquery": "2.0.0", "jquery-1.9.1": "code.jquery.com/jquery-1.9.1.js" } ---------- This answer is working fine.But when I minify and uglify the code only one version is available and so the code which use other version is getting broken.Is there any solution for this?Unicorn
O
158

According to the bower docs

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>

You can install two different versions of jQuery like so:

bower install jquery-legacy=jquery#1.10 jquery-modern=jquery#2

Or, if you prefer to set that up in a bower.json

"dependencies": {
    "jquery-legacy": "jquery#1.10",
    "jquery-modern": "jquery#2"
}
Olid answered 14/8, 2013 at 20:25 Comment(5)
thanks! good to know, is that a newly added feature? I dont think this was a possibility when I was originally seeking an answerGundry
@AdamCoulombe looks like this was added as of v1.0.0. It's been stable for a relatively long period of time (software wise) but wasn't in any pre-release of bower.Olid
This should be the chosen answerGrunenwald
Definitely this should be the answerDyslogia
This really helps. Follow up question: how do you specify which jQuery version needs to be used on another library? I have an open question here.Hispania
L
67

In the dependencies part of your bower.json you can have something like this:

"dependencies": {
    "jquery": "2.0.0",
    "jquery-1.9.1": "http://code.jquery.com/jquery-1.9.1.js"
}

One shouldn't normally have to do this, but sometimes you have to maintain / migrate an existing website that (for whatever reason) uses different versions of jquery in different pages!

Lifeblood answered 15/5, 2013 at 20:17 Comment(2)
what about: "jquery-1.9.1": "jquery#1.9.1"Magnifico
"dependencies": { "jquery": "2.0.0", "jquery-1.9.1": "code.jquery.com/jquery-1.9.1.js" } ---------- This answer is working fine.But when I minify and uglify the code only one version is available and so the code which use other version is getting broken.Is there any solution for this?Unicorn
I
12

From the command line, if you just want the latest 1.x and 2.x versions, you can loosen the constraints in the answer above.

So:

bower install jquery-legacy=jquery#1.10 jquery-modern=jquery#2

would become:

bower install jquery-legacy=jquery#^1 jquery-modern=jquery

Ishmael answered 25/7, 2014 at 15:6 Comment(1)
As the PHP/Packagist community is now learning, bower install jquery-legacy=jquery#^1 jquery-modern=jquery -- while looser-- still doesn't place an upper bound on the "modern" version. A better loose constraint would be: bower install jquery-legacy=jquery#^1 jquery-modern=jquery#^2, since it keeps the modern with an upper bound to prevent major breaking changes. By the time 2.x isn't "modern" anymore, i'd assume you would change the package name anyway.Olid
A
0

bower.json:

This is how i did it...

"dependencies": {
    ...
    "jquery": "2.0.0",
    "jquery-old": "1.9.1"
    ...
}

Second version, can be any version, old or new. You just have to add a different key. Like jquery-old

Install

bower install --save jquery-old

Use

Now you can use either one of the jquery version:

<script type="text/javascript" src="path/to/bower/directory/jquery/dist/jquery.min.js"></script>

<script type="text/javascript" src="path/to/bower/directory/jquery-old/dist/jquery.min.js"></script>

Bonus

"dependencies": {
    ...
    "jquery": "2.0.0",
    "jquery-old": "1.9.1"
    "jquery-latest": "^3.3.1"
    ...
}

Argentina answered 21/6, 2018 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.