Determining installed plugins at runtime for Cordova and PhoneGap applications

This post is more than 2 years old.

Earlier today on Twitter a user asked an interesting question: How can I tell - via JavaScript - if a particular plugin is installed in a Cordova/PhoneGap application. I responded by asking how you wouldn't know since it is your own app, but then he mentioned that his code base was stand alone and would be used within other projects. (So basically - just JavaScript code that other Cordova/PhoneGap applications would use.)

There are a couple of different ways to handle this. The first, and simplest, is to look for the "hook" the plugin adds. For example, the barcode plugin adds methods to a cordova.plugins.barcodeScanner object. It would be trivial to see if that exists. Cool, problem solved, right?

Well in his case he was using InAppBrowser. This plugin simply modifies window.open so it isn't something you can really (as far as I can see) introspect. I did some more digging and found something interesting.

If you look at the platform build version of your www code, you will notice it includes a cordova_plugins.js file:

If you open it up, you will see a list of any plugins you have installed. I opened up cordova.js (honestly, I don't look at it often, but I should) and saw that cordova_plugins.js was being loaded in dynamically and parsed. My assumption is that this has to happen before deviceready fires so you can safely use any plugins you've got installed. On a whim then I tried the following code inside my deviceready:

var md = cordova.require("cordova/plugin_list").metadata;

The metadata part came from what I saw in cordova_plugins.js. While the rest of the file has random stuff based on the plugins installed, metadata appears to be just a list of your plugins. I confirmed that this worked well:

So - that's it. I should note that I spoke with Shazron and he mentioned that if you used browserify, it might mess with how the JS file is generated. I'd say use with caution and let me know (via the comments) how it works for you.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Nic Raboy posted on 11/20/2014 at 7:37 PM

This worked exactly how I wanted.

Thanks for this!

Comment 2 by Nic Raboy posted on 11/23/2014 at 1:01 AM

I received a report that it is causing the users device to freeze.

https://twitter.com/RamizKa...

Any ideas on why this might be happening?

Thanks,

Comment 3 (In reply to #2) by Raymond Camden posted on 11/23/2014 at 3:46 AM

Nope. But I'll test. It may be a while though - entering vacation time.

Comment 4 by Raymond Camden posted on 11/23/2014 at 2:48 PM

Please forgive my test - this is OT and can be ignored. Nic, if you get an email on this, let me know if the link works.

Comment 5 (In reply to #4) by Raymond Camden posted on 11/23/2014 at 3:02 PM

One more quick test.

Comment 6 by Michal Mocny posted on 11/23/2014 at 9:19 PM

Speaking of browserify.. would love to hear when/how that is being used. Effort seems to have dropped off lately.

Comment 7 (In reply to #6) by Raymond Camden posted on 11/24/2014 at 2:21 PM

I only "kinda" know what browserify does. I haven't used it personally.

Comment 8 (In reply to #3) by Raymond Camden posted on 11/24/2014 at 2:42 PM

Unfortunately I couldn't reproduce this. I tested in Genymotion (simulator) and my HTC M8. It did not hang. Best I can suggest is to reproduce it yourself and try to see where in particular it gets hung up.

Comment 9 (In reply to #8) by Nic Raboy posted on 11/24/2014 at 6:23 PM

The user later mentioned this only happened outside the onDeviceReady() or $ionicPlatform.ready() methods. I think this is a non-issue as of now.

Thanks for looking into it.

Comment 10 by Joris Witteman posted on 9/3/2015 at 9:53 AM

I'm using Ionic Framework with their Deploy service, which allows you to live-ship an updated JS codebase to users who already have your app installed.

This poses the risk of deploying an JS codebase with an added cordova plug-in, where the app binary on the user's phone is missing the platform-native counterpart of that plugin.

Your method may be a life saver in this regard, as it would allow me to detect if the user's binary does in fact have the plug-in installed. My only question is, do you know for sure that this list is based on what plugins the platform side of the app has installed? Or is it just a list of plug-ins present in the JS codebase?

Comment 11 (In reply to #10) by Raymond Camden posted on 9/3/2015 at 10:45 AM

It should work. I'm not 100% certain. You could test easily though, right? :)

Comment 12 (In reply to #11) by Joris Witteman posted on 9/3/2015 at 12:40 PM

Yeah it definitely lists the plugins that were present at build time, effectively giving you the list of plugins that the binary being run, supports.

Comment 13 (In reply to #12) by Raymond Camden posted on 9/3/2015 at 1:15 PM

Of course - I wasn't worried at all. ;)

Comment 14 by Ben Wilson posted on 6/10/2016 at 11:49 PM

Very helpful thanks again Ray!

Comment 15 (In reply to #12) by Antonio Gallo posted on 9/23/2016 at 10:22 AM

:-( i wish i had seen this post before

Comment 16 (In reply to #13) by Antonio Gallo posted on 9/23/2016 at 10:23 AM

why this isn't documented officially ? :-P damn its a live saver... by mistake its very easy to push an app to the store with some missing or incorrect plugin :-P

Comment 17 (In reply to #16) by Raymond Camden posted on 9/23/2016 at 11:31 AM

This is why you always test. :)

Comment 18 by Amila Jayasinghe posted on 10/11/2016 at 6:12 AM

Hi Raymond,

I had a look at the "cordova_plugins.js" and I found that "cordova-plugin-email-composer" was already listed in. So I used simple coding below after the 'deviceready' in order to make sure that it loads at the runtime in phonegap simulater.
/*********
$scope.goto_contactus = function(){
if (window.cordova && cordova.plugins.email) {
console.log('True');
} else {
console.log('False');
}
}
*********/
But the result was 'True' in the browser and it was 'False' at the Phonegap simulater app.

The code inside the "cordova_plugins.js" was as below.
/****************
"id": "cordova-plugin-email-composer.EmailComposer",
"file": "plugins/cordova-plugin-email-composer/www/email_composer.js",
"pluginId": "cordova-plugin-email-composer",
"clobbers": [
"cordova.plugins.email",
"plugin.email"
]
******************/
Do you have any idea, why is the plugin doesn't load when I run it on simulater ?
I already tried some other plugins and those were worked perfectly. But it doesn't work for "cordova-plugin-email-composer"

Thanks

Comment 19 (In reply to #18) by Raymond Camden posted on 10/11/2016 at 12:54 PM

To be clear - the plugin works, just not your code to check for it?

Comment 20 (In reply to #9) by Antonio Gallo posted on 10/11/2016 at 1:13 PM

surely... it must used AFTER device ready :)

Comment 21 (In reply to #19) by Antonio Gallo posted on 10/11/2016 at 1:14 PM

As soon as i'll have some free time i'll post the code of a service of mine to check plugins at runtime based on the undocumented method described by you :) Thanks as always :)

Comment 22 (In reply to #8) by Antonio Gallo posted on 10/14/2016 at 8:31 AM

i got it with ios simulator :)