76

I noticed that the builds on our CI started to fail because of the following error:

Discovered plugin "cordova-plugin-app-version" in config.xml. Adding it to the project

Installing "cordova-plugin-app-version" for android

Adding cordova-plugin-app-version to package.json
Using "requireCordovaModule" to load non-cordova module "q" is not supported. Instead, add this module to your dependencies and use regular "require" to load it.
[ERROR] Exception: 
The command '/bin/sh -c ionic cordova platforms add android' returned a non-zero code: 1

Same issue happens on both iOS and Android.

After some digging, I found that cordova 9.0.0 was recently released.

There are a couple of changes related to this issue.

  • GH-710 Drop Q Dependency and Use Native Promises
  • GH-707 Deprecate requireCordovaModule for non-Cordova modules

My first thought was that somehow we didn't have our cordova version locked, but we did lock it to cordova@8.1.2 in our dockerfile. I also tested it on my local machine and I cordova version 8.1.2 as well. So this can't be the issue.

After looking at the logs some more, I noticed some logs like these:

cordova-android version check failed ("/app/platforms/android/cordova/version"), continuing anyways.

There were a couple of those, like 3 or 4, but the build did not stop when that happened.

I then ran the build of an older commit again, and it worked fine, probably because some layers were cached. But if I changed only a single package (I updated prettier to try it out), it caused most of the layers to be rebuilt and the build crashed with the above error.

It seems that somehow some dependencies deeper down got updated, which are causing the issue.

Thanks for your help.

Sean Lintern
  • 3,103
  • 22
  • 28
Andreas Gassmann
  • 6,334
  • 7
  • 32
  • 45

15 Answers15

136

I have returned to the previous version: 8.1.2.

npm install -g cordova@8.1.2

Now, it's working again.

If you want to stay on the latest version of cordova, go to the following instructions:
https://stackoverflow.com/a/58956882/9536897

Or Choban
  • 1,661
  • 1
  • 8
  • 9
  • 3
    This doesn't work for me, I already have cordova 8.1.2 installed, but I'm still getting the error. – Andreas Gassmann Mar 24 '19 at 13:52
  • 3
    That worked for me. Thank you. Also, note to others if you cordova platform remove make sure to cordova platform add at the version that works for you. – SaundersB Mar 24 '19 at 18:04
  • 4
    This is not a solution, it's a workaround. It worked for me. Guess we have to wait until cordova developers fix it. – Martin K Mar 26 '19 at 09:48
  • 1
    Bless you! Apart from this issue. If you are trying to create IOS project: use "cordova platform add ios@4.5.5" as IOS platform – Fuat Jul 27 '19 at 17:41
  • If we downgrade cordova 9.0.0 to 8.1.2 then how i get support android-sdk: 28.Because playstore need android-sdk 28 – Utpaul Aug 08 '19 at 10:35
  • Also, this could probably be linked with this ticket too: https://github.com/xpbrew/cordova-sqlite-storage/issues/856 – vr_driver Feb 25 '20 at 13:49
  • sir i tried above solution but cordova is not installed it will hold for a min and not execute fully can you help me what ido? – Kapil Soni Jun 07 '20 at 06:27
  • This solution is valid if you are using ionic 3 – Basem Olimy Oct 24 '20 at 11:16
15

Simple:

Replace the requireCordovaModule to require :

requireCordovaModule("q") to require("q")

Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
14

i can solve this issue by downgrading cordova to 8.1.1.

npm remove cordova -g && npm install -g cordova@8.1.1
Alongkorn
  • 3,968
  • 1
  • 24
  • 41
10

This solution worked for me - https://github.com/xpbrew/cordova-sqlite-storage/issues/856#issuecomment-497298630

For whatever plugin this error is associated with, run:

cordova platform rm ios
cordova platform rm android
cordova plugin rm <package-name>

npm i <package-name>@latest
cordova plugin add <package-name>
cordova platform add ios
cordova platform add android

edit: I recently discovered that running:

cordova platform add ios && cordova platform add android

would not install the most recent versions, would be nice for someone to explain why this is different to running them seperately, but this was my problem.

Maximillion Bartango
  • 1,533
  • 1
  • 19
  • 34
8

I ran into this also. In my case, I needed to remove a problematic plugin cordova-plugin-camera-preview which lists "cordova": "*" as a dependency. This would install cordova 9.0.0 during ionic cordova build

mattdiep
  • 96
  • 1
  • That might actually be the issue in my case. I'll get back to you when I know more. – Andreas Gassmann Mar 25 '19 at 10:20
  • This is a red-herring. The dependency on cordova:* is not required by the plugin, but all it means is that you will have the global cordova installed and there will be an unused cordova@latest inside your node_modules folder of the project. – purplecabbage Mar 25 '19 at 20:59
  • 1
    It does seem that the ionic cli will use the version in node_modules if available. https://github.com/ionic-team/ionic-cli/commit/fe072159a9cc1ecd140c1abfbf64f1a7d9ae2a6f Running `ionic info` before the plugin is added shows the globally installed cordova version (8.1.2 in my case), running it after shows @latest (9.0.0). Running `cordova -v` both before and after shows 8.1.2 as expected. Removing the plugin fixes the issue (and removes cordova from node_modules/.bin) – mattdiep Mar 26 '19 at 18:43
  • 2
    Even though the fix for most people is to simply downgrade to 8.1.2, I clearly stated in my question that I have fixed my cordova version to 8.1.2 and it still didn't work. The reason was some odd combination of the camera-preview plugin and ionic, which basically just ignored the defined cordova version and just used 9.0.0 regardless. So it turns out that in my specific case, it was related exactly to this issue, which is why this is the accepted answer. – Andreas Gassmann May 15 '19 at 08:51
8

Seems to be an issue with cordova 9.0.0, see this issue ticket.

Looks like it is already fixed and will be included in the next (9.0.1?) release.

Dominic
  • 4,572
  • 3
  • 25
  • 36
8

That's it, I found it. It comes from an update to cordova cli @9.0.0 and it's append on existing projects.
The issue come from cordova-android-support-gradle-release and fixed in version latest.

So in any case, what you need to do is:
cordova plugin rm cordova-android-support-gradle-release
cordova plugin add cordova-android-support-gradle-release@latest

Or Choban
  • 1,661
  • 1
  • 8
  • 9
  • 1
    The issue comes from the `...\plugins\cordova-android-support-gradle-release\scripts\apply-changes.js` and was fixed in the new version. – Or Choban Nov 20 '19 at 14:38
  • 1
    This particular plugin wasn't the problem. The solution was to update ALL cordova plugins, so thanks for the hint – mwarren May 15 '20 at 15:03
  • Unfortunately, trying to run `cordova plugin rm ...` gives the same error. – Stijn de Witt Sep 09 '22 at 07:32
6

I also get this error after updating to 9.0.0. In my case I fixed it doing a clean up and rebuilding my app. You can try the following steps.

First of all

  1. Update Cordova: npm install -g cordova
  2. Update SDK, ex: ./android/tools/bin/sdkmanager "build-tools;28.0.3" "platforms;android-28"
  3. Add SDK path to your system (1), ex: in bashrc export ANDROID_SDK_ROOT=~/android

(1) ANDROID_HOME is deprecated but still working

In your Cordova Project

  1. Remove node_modules, platforms and plugins folders
  2. Remove package.json and package-lock.json
  3. Remove from config.xml plugins/platforms/engine
  4. Add your platform, ex: cordova platform add android
  5. Add your plugins updated, ex: cordova plugin add cordova-plugin-whitelist
  6. ex: npm install
  7. Build: cordova build

You can check a resume of your app with: cordova info

If you want to see a list of your plugin you can use: cordova plugins ls

Hope this help or at least point someone to the right direction :)

Grenoblois
  • 503
  • 1
  • 5
  • 19
gengns
  • 1,573
  • 14
  • 23
5

As the error says, you need to require the q. Go to npmInstall.js (Search the requireCordovaModule in your project then you will see the npmInstall.js ).

Then, add these lines;

var q = require('q');
var npmModule = require('npm');

and replace: var Q = context.requireCordovaModule('q'); with

var Q = context.q;

and replace: var npm = context.requireCordovaModule('npm'); with

var npm = context.npmModule;
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
erdemgunenc
  • 969
  • 3
  • 14
  • 25
4

This is what worked for me: I removed cordova by calling

npm uninstall cordova -g

and then installed it globally again with

npm install -g cordova@latest
Or Choban
  • 1,661
  • 1
  • 8
  • 9
Bonfix Ngetich
  • 1,087
  • 11
  • 15
  • This worked for me. Thank you very much. (i had cordova 9.0) – Andrei Telteu Aug 07 '20 at 00:13
  • When I uninstall cordova 10.0.0 and then install cordova 8.1.2, and then see the version of corodva by command cordova -v. The terminal will appear some warnings as below: Does somebody have a solution? (node:17636) Warning: Accessing non-existent property 'cat' of module exports inside circular dependency (Use `node --trace-warnings ...` to show where the warning was created) (node:17636) Warning: Accessing non-existent property 'cd' of module exports inside circular dependency – Akira Chen Sep 02 '21 at 03:25
  • @AkiraChen I'm facing the same issue, If you have found a work around , Please let me know – RHOOPH Aug 18 '22 at 10:18
  • OK. I will do it. – Akira Chen Oct 18 '22 at 01:45
0

remove cordova-plugin-crosswalk-webview

  • 2
    I understand the downvote (not the plugin that the OP was talking about), but this helped me because crosswalk was the issue that caused this error for me. – B-Rad Jun 21 '19 at 00:23
  • 1
    I agree. In order to find out which plugin is causing the error, you must run the command with `--verbose`. – Laura Jul 10 '19 at 12:40
  • package.json and package-lock.json must both be modified? – Akira Chen Sep 30 '21 at 08:24
0

I had the same issue. This is how I solved mine:

cordova plugin | awk '{ print "cordova plugin rm " $1 " && cordova plugin add " $1 }' | sh
ionic cordova remove android && ionic cordova add android
0

C:\Windows\System32 to my PATH variable to fix this odd issue.

Supriya
  • 481
  • 5
  • 5
0

Had same issue in my project.

One thing for sure that the function requireCordovaModule is not deprecated (for now)

I use CI, and in pipelines it was giving error. Many forms suggest to downgrade cordova to 8x (BTW updated to 9.0.0 when i got this error) .. it works but again downgrading is never good.

One of the step i have in pipelines is ionic cordova build which i replaced with ionic cordova run without any change in code it works

Shanu ji
  • 359
  • 4
  • 5
0

My solution was to edit the two files

...\node_modules\cordova-admob\scripts\100-prepare-admob-angular.js
...\plugins\cordova-admob\scripts\100-prepare-admob-angular.js

and replace

new context.requireCordovaModule('q') 

with

require('q')
Arne M
  • 87
  • 8