josemigallas
josemigallas

Reputation: 3918

How to use NPM scoped packages for a Cordova plugin

we are developing a couple of NPM packages under the same organization:

@aerogearservices/core
@aerogearservices/core-rn
@aerogearservices/core-cordova

Where core-cordova is a Cordova plugin, installed via dependencies (read more about scoped packages).

Relevant files:

App's package.json

{
  ...
  "dependencies": {
    "@aerogearservices/core-cordova": "1.0.0",
    ...
  },
  "cordova": {
    "plugins": {},
    "platforms": ["ios", "android"]
  }
}

Plugin's package.json

{
  "name": "@aerogearservices/core-cordova",
  "version": "1.0.0",
  ...
  "cordova": {
    "id": "aerogearservices-core-cordova",
    "platforms": [
      "android"
    ]
  },
  "dependencies": {
    "@aerogearservices/core": "1.0.0"
  },
  "engines": {
    "cordovaDependencies": {
      ...
    }
  }
}

Plugin's plugin.xml

... ... Apache 2.0

<platform name="android">
    <config-file target="res/xml/config.xml" parent="/*"> 
        ...
    </config-file>

    <source-file 
        ...
    />
</platform>

The plugin really does nothing yet, I am just trying to install it in my app but when I run cordova platform add android I get this error:

UnhandledPromiseRejectionWarning: CordovaError: Cannot find plugin.xml for plugin "@aerogearservices". Please try adding it again.

The problem is that Cordova installs the plugin under myApp/plugins/aerogearservices/core-cordova so that it is looking for plugin.xml at the wrong location.

enter image description here

How do scoped packages and cordova plugins work together? Is there any workaround to this without renaming the plugin?

Upvotes: 9

Views: 1090

Answers (2)

Hiroyuki Okada
Hiroyuki Okada

Reputation: 51

You may have already solved this problem, but cordova@10 can handle scoped plugins just fine.

Plugin's config.xml should mention the scope:

<plugin id="cordova-plugin-myplugin" ...>
<name>@scope/cordova-plugin-myplugin</name>

And, plugin's package.json should mention it:

"name": "@scope/cordova-plugin-myplugin",
"cordova": {
    "id": "@scope/cordova-plugin-myplugin",
    "platforms": [
        "android",
        "windows",
        "ios"
        ]
    },

now, I have no problem using

cordova plugin add @scope/cordova-plugin-myplugin

Upvotes: 3

G&#233;raud Vercasson
G&#233;raud Vercasson

Reputation: 71

It might not be the slution to your problem, but I ended up having problems creating a cordova plugin published in a scoped npm registry, and this is how I managed to fix it:

Plugin's config.xml should not mention the scope:

<plugin id="cordova-plugin-myplugin" ...>
<name>cordova-plugin-myplugin</name>

On the other hand, plugin's package.json should mention it:

"name": "@scope/cordova-plugin-myplugin",
"cordova": {
    "id": "@scope/cordova-plugin-myplugin",
    "platforms": [
        "android",
        "windows",
        "ios"
        ]
    },

now, I have no problem using

cordova plugin add @scope/cordova-plugin-myplugin

I hope this might actually help someone

Upvotes: 7

Related Questions