How to use NPM scoped packages for a Cordova plugin
Asked Answered
M

2

9

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?

Margarettemargarida answered 2/4, 2018 at 12:35 Comment(1)
Patch Cordova and submit PR?Leotaleotard
N
7

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

Novena answered 15/11, 2018 at 15:15 Comment(0)
W
3

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
Wain answered 27/12, 2020 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.