cordova
, the command line interface to the Cordova development framework, is a node package. npm
, the node package manager, has the concept of a ‘global’ install and ‘local’ installs. Typical installs of the cordova command line interface instruct you to perform a global installation. That is what the -g
in npm install -g cordova
(from the docs) means. Installing globally means that one version of cordova is available to every user on that computer, and will be installed in a system directory (for example, /usr/local/lib/node_modules
, on linux). If you upgrade that one version, everyone and every project on the system will be upgraded (or forced to upgrade, depending on your perspective).
If you want to run different versions of cordova for different projects, you have to install locally. That means that all the cordova code will be installed in a local directory, rather than a system wide directory.
Why might you want do this? Any number of reasons, but primarily because the relationship between your application and a particular version of Cordova is important.
- If your application depends on bugs or feature behavior that has changed between versions, you could need to freeze your application at a given version of Cordova, at least until you were able to update your application and test it against a newer version.
- You could have multiple applications that were developed at different times, and the older ones could crash if upgraded (or perhaps you don’t have the time to test against the latest cordova version).
- Applications under active development may be at the latest and greatest version of cordova, while the others may be sitting at what was the latest and greatest when they were last modified.
- A local install will let you test your application against a new version of cordova without committing a global install, which might affect other team members, other projects, or necessitate a downgrade if the new version has issues.
- You might be working on a platform that is not amenable to being place in a virtual machine (like iOS) and yet still want to run different versions of Cordova on the same machine.
Below, I outline steps to follow if you want to have one project using Cordova 3.3 and another using Cordova 3.1. npm view cordova
lets you view the available versions–typically you want the latest minor revision (3.3.1-0.1.2 is better than 3.3.0-0.1.0). These are steps for Android, but it would be similar for any target platforms. And these steps assume you have successfully installed the target platform tools for Android previously.
mkdir cordova3.3 # or cordova3.1 cd cordova3.3 # or cordova3.1 npm install cordova@3.3.1-0.1.2 # or 3.1.0-0.2.0 mkdir project cd project ../node_modules/cordova/bin/cordova create test33 io.cordova.HelloWorld33 # or test31 io.cordova.HelloWorld31 cd test33 #(or test31) ../../node_modules/cordova/bin/cordova platform add android ../../node_modules/cordova/bin/cordova plugin add org.apache.cordova.device
Don’t forget to add the device feature to your config.xml
<feature name="Device"> <param name="android-package" value="org.apache.cordova.device.Device" /> </feature>
You need to update the www/js/index.js
file to display your Cordova version. This is only to prove you have different versions of Cordova running on the same machine
onDeviceReady: function() { alert(device.cordova); //app.receivedEvent('deviceready'); },
Finally build the application and run it on your emulator:
emulator -avd avdname # start a previously created avd via the android command ../../node_modules/cordova/bin/cordova build android ../../node_modules/cordova/bin/cordova emulate android
Note that there should be no issues with running applications built with different versions of cordova on the same device–the applications are all sandboxed.
If you are often in this situation, you can create aliases that point to the correct version of the cordova
command, since the results are indeterminate if you mix and match calls to different versions. For example:
alias cordova31='/path/to/cordova3.1/node_modules/cordova/bin/cordova' alias cordova33='/path/to/cordova3.3/node_modules/cordova/bin/cordova'
Incidentally, I tried these steps out with the latest version of the android tools (version 19 of the Android build tools) with all four of the latest minor revisions of cordova: 3.0.10
, 3.1.0-0.2.0
, 3.2.0-0.4.0
, and 3.3.1-0.1.2
, and could only get the 3.1.x and 3.3.x versions to work. So another reason to do a local install is to perform a quick check to see if your platform installation is compatible with a given version of Cordova.
Do you have different versions of Cordova installed on the same machine?
Thank you very much! Your instructions are really useful for me!
Bye, Gio
Hi,
do you know also how to set the ionic CLI to work with different versions of Cordova?
Thanks!
Hi Sergio,
Have you tried just installing different versions of ionic?
I haven’t used ionic, but from here: https://github.com/driftyco/ionic-cli
It looks like just another NPM app.
Thanks Dan,
I’ve installed the ionic CLI but the problem is that ionic internally calls the cordova command and that is not found because I’ve installed the cordova CLI as per your post.
I tried to set a symbolic link to cordova but it doesn’t work.