Cordova error: Using "requireCordovaModule" to load non-cordova module "q" is not supported
Asked Answered
E

15

77

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.

Elegy answered 22/3, 2019 at 16:33 Comment(0)
A
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

Azzieb answered 24/3, 2019 at 21:9 Comment(4)
That might actually be the issue in my case. I'll get back to you when I know more.Elegy
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.Schnook
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)Azzieb
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.Elegy
R
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://mcmap.net/q/264546/-cordova-error-using-quot-requirecordovamodule-quot-to-load-non-cordova-module-quot-q-quot-is-not-supported

Reld answered 24/3, 2019 at 8:19 Comment(8)
This doesn't work for me, I already have cordova 8.1.2 installed, but I'm still getting the error.Elegy
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.Talkington
This is not a solution, it's a workaround. It worked for me. Guess we have to wait until cordova developers fix it.Clavicytherium
Bless you! Apart from this issue. If you are trying to create IOS project: use "cordova platform add [email protected]" as IOS platformAddam
If we downgrade cordova 9.0.0 to 8.1.2 then how i get support android-sdk: 28.Because playstore need android-sdk 28Spies
Also, this could probably be linked with this ticket too: github.com/xpbrew/cordova-sqlite-storage/issues/856Conchoid
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?Hibbert
This solution is valid if you are using ionic 3Bridewell
U
16

Simple:

Replace the requireCordovaModule to require :

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

Unwell answered 2/10, 2019 at 5:41 Comment(4)
where to replace this ? at which file ?Riocard
Usually you need to change in plugin file. You have to check error refresh so can find where you need to replace.Unwell
I modified the hooks/beforedep.js and hookers.js. But the error still appears when I execute the command line: cordova build android --release --buildConfigDilettante
Ctrl+Shift+F in the <your-project-root>/plugins folder for requireCordovaModule to identify which plugins (subfolders) are outdatedStaffer
H
14

i can solve this issue by downgrading cordova to 8.1.1.

npm remove cordova -g && npm install -g [email protected]
Heliometer answered 4/1, 2020 at 7:0 Comment(0)
W
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.

Wheal answered 8/7, 2019 at 14:37 Comment(0)
R
9

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

Reld answered 20/11, 2019 at 14:37 Comment(3)
The issue comes from the ...\plugins\cordova-android-support-gradle-release\scripts\apply-changes.js and was fixed in the new version.Reld
This particular plugin wasn't the problem. The solution was to update ALL cordova plugins, so thanks for the hintPussyfoot
Unfortunately, trying to run cordova plugin rm ... gives the same error.Grasping
A
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

Azzieb answered 24/3, 2019 at 21:9 Comment(4)
That might actually be the issue in my case. I'll get back to you when I know more.Elegy
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.Schnook
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)Azzieb
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.Elegy
N
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.

Natator answered 2/4, 2019 at 12:17 Comment(1)
Getting the issue today, sep 9 2022 :(Grasping
P
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 :)

Perceptible answered 2/4, 2019 at 14:42 Comment(1)
Cannot find module 'properties-parser'Modern
U
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;
Upperclassman answered 10/5, 2020 at 23:21 Comment(0)
A
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
Antitank answered 3/9, 2019 at 9:17 Comment(4)
This worked for me. Thank you very much. (i had cordova 9.0)Wanting
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 dependencyDilettante
@AkiraChen I'm facing the same issue, If you have found a work around , Please let me knowSlapdash
OK. I will do it.Dilettante
R
0

remove cordova-plugin-crosswalk-webview

Rush answered 15/4, 2019 at 14:59 Comment(3)
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.Limiting
I agree. In order to find out which plugin is causing the error, you must run the command with --verbose.Ailanthus
package.json and package-lock.json must both be modified?Dilettante
L
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
Lalittah answered 12/11, 2019 at 4:15 Comment(0)
P
0

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

Patricepatrich answered 12/11, 2019 at 7:38 Comment(0)
O
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

Only answered 7/1, 2021 at 20:23 Comment(0)
C
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')
Cutlip answered 25/3, 2023 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.