Search code examples
javascriptandroidcordovameteorcordova-plugins

cordova/exec fails within meteor project


After building a Cordova plugin with plugman, installing it in meteor 2.3.2 and running meteor run android-build, I get the following error from within the plugin:

Unable to resolve some modules:

  "cordova/exec" in ~/MeteorApp/MyPlugin/www/MyPlugin.js (web.browser)

The error line is from line 2 in the generate MyPlugin.js

// ~/MeteorApp/MyPlugin/www/MyPlugin.js


// I added this check for cordova myself

if (cordova && cordova.exec ) {
    var exec = require('cordova/exec');
    exports.coolMethod = function (arg0, success, error) {
        exec(success, error, 'MyPlugin', 'coolMethod', [arg0]);
    };
}

My meteor structure includes the plugin folder at the route of the app

I installed the package using meteor add cordova:my.plugin.id@file://path/to/plugin/project

Cordova version 10.0.8

I have the following plugin structure:

pluginDir/
   src/
      android/
         MyPlugin.java
   www/
      MyPlugin.js
   package.json
   plugin.xml

I have tested the plugin inside a Cordova app and that works just fine.

I bet the issue here is how meteor exposes the internal Cordova object.


Solution

  • Solution

    Meteor already wraps the cordova object so really instead of importing cordova/exec in the /www plugin definition I simply had to use cordova.exec('my logic goes here')

    The above is scaffolded with plugman and the /www folder and files get generated automatically, as well as the above code.

    The code above is valid within Cordova, but this logic fails within Meteor.

    Extra comments

    Only Android support was required for this plugin and the cordova.exec() logic was moved into a Meteor api method.

    Because of this Android-only requirement the /www folder and references was removed.

    The /www folder may be reintroduced in the future in order to define a nice fallback for web usage scenarios, but it's unlikely for our internal plugin.