Cordova 7 - config.xml or package.json?

As an evolution to this question, it looks like Cordova 9.0.0 has decided to stop syncing the config.xml and package.json files now. So the package.json is the primary location for platform/plugin info, which seems very strange to anyone with significant Cordova experience.

I didn't see this announced anywhere obvious, but stumbled into it while using 9.0.0, and confirmed below:

  • https://cordova.apache.org/announcements/2019/03/18/cordova-lib-release-9.0.0.html

Changes include: GH-750 Remove saving platforms/plugins to config.xml

  • https://github.com/apache/cordova-lib/pull/750

EDIT: This is no longer accurate, as of Cordova 9. There is an issue to document this in the docs, which has not been addressed yet. It seems as currently the plan is to migrate away from config.xml entirely, however that does not seem to be complete.

See also this answer.


From what i can read at the Cordova 7 Release Notes, the package.json will always be created to mirror the config.xml whenever you run cordova prepare, if it does not exist. If a package.json does exist, it will take preference (but only for the things defined in it, like plugins and platforms, for other config options config.xml will still be used).

So, it might be a solution for you to save your settings in config.xml, and always delete the package.json before running cordova prepare, maybe with a custom npm script.


For the version number, you can store that in package.json and add an npm script named "version" that updates config.xml accordingly. Then always use npm version instead of manually changing the version number.

From the blog of masimplo:

package.json

  "scripts": {
    ...
    "version": "./bin/update-config-version.sh",
    ...
  },

update-config-version.sh

#!/bin/bash

CONFIG='config.xml'
NEW_VERSION=${npm_package_version}

if [ -e $CONFIG ]; then
    # sed to replace version in config.xml
    sed -i '' "s/\(widget.*version=\"\)\([0-9,.]*\)\"/\1$NEW_VERSION\"/" $CONFIG
    git add $CONFIG
    echo "Updated $CONFIG with version $NEW_VERSION"
else
    echo 'Could not find config.xml'
    exit 1
fi

There's also an npm package. There's always an npm package. Use like so:

npm install --save-dev cordova-set-version

package.json

  "scripts": {
    ...
    "version": "cordova-set-version --version ${npm_package_version} && git add config.xml",
    ...
  },

It doesn't fix most of the duplication, but makes it slightly more bearable. Edit: cordova-set-version also reformats your config.xml and removes all comments from it. I filed an issue.