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 [email protected] 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.

15 Answers 15

136

I have returned to the previous version: 8.1.2.

npm install -g [email protected]

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

8
  • 3
    This doesn't work for me, I already have cordova 8.1.2 installed, but I'm still getting the error. Mar 24, 2019 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, 2019 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, 2019 at 9:48
  • 1
    Bless you! Apart from this issue. If you are trying to create IOS project: use "cordova platform add [email protected]" as IOS platform
    – Fuat
    Jul 27, 2019 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 8, 2019 at 10:35
15

Simple:

Replace the requireCordovaModule to require :

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

4
  • 7
    where to replace this ? at which file ?
    – Lejiend
    May 2, 2020 at 4:23
  • Usually you need to change in plugin file. You have to check error refresh so can find where you need to replace. Jul 16, 2020 at 16:52
  • 1
    I modified the hooks/beforedep.js and hookers.js. But the error still appears when I execute the command line: cordova build android --release --buildConfig
    – Akira Chen
    Sep 2, 2021 at 3:19
  • Ctrl+Shift+F in the <your-project-root>/plugins folder for requireCordovaModule to identify which plugins (subfolders) are outdated Nov 4, 2022 at 20:52
14

i can solve this issue by downgrading cordova to 8.1.1.

npm remove cordova -g && npm install -g [email protected]
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.

0
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

4
  • That might actually be the issue in my case. I'll get back to you when I know more. Mar 25, 2019 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. Mar 25, 2019 at 20:59
  • 1
    It does seem that the ionic cli will use the version in node_modules if available. github.com/ionic-team/ionic-cli/commit/… 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, 2019 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. May 15, 2019 at 8: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.

1
  • Getting the issue today, sep 9 2022 :( Sep 9, 2022 at 7:31
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

3
  • 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, 2019 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, 2020 at 15:03
  • Unfortunately, trying to run cordova plugin rm ... gives the same error. Sep 9, 2022 at 7: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 :)

1
  • Cannot find module 'properties-parser'
    – Grenoblois
    Jan 2, 2021 at 20:16
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;
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
4
  • This worked for me. Thank you very much. (i had cordova 9.0) Aug 7, 2020 at 0: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 2, 2021 at 3:25
  • @AkiraChen I'm facing the same issue, If you have found a work around , Please let me know
    – RHOOPH
    Aug 18, 2022 at 10:18
  • OK. I will do it.
    – Akira Chen
    Oct 18, 2022 at 1:45
0

remove cordova-plugin-crosswalk-webview

3
  • 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, 2019 at 0: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, 2019 at 12:40
  • package.json and package-lock.json must both be modified?
    – Akira Chen
    Sep 30, 2021 at 8: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.

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

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.