Andreas Gassmann
Andreas Gassmann

Reputation: 6544

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

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.

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.

Upvotes: 77

Views: 96556

Answers (15)

Arne M
Arne M

Reputation: 97

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

Upvotes: 0

Bonfix Ngetich
Bonfix Ngetich

Reputation: 1237

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

Upvotes: 4

Shanu ji
Shanu ji

Reputation: 359

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

Upvotes: 0

gengns
gengns

Reputation: 1633

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

Upvotes: 6

erdemgunenc
erdemgunenc

Reputation: 997

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;

Upvotes: 5

Maximillion Bartango
Maximillion Bartango

Reputation: 1599

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.

Upvotes: 11

Alongkorn
Alongkorn

Reputation: 4197

i can solve this issue by downgrading cordova to 8.1.1.

npm remove cordova -g && npm install -g [email protected]

Upvotes: 14

Or Choban
Or Choban

Reputation: 1671

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

Upvotes: 136

Or Choban
Or Choban

Reputation: 1671

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

Upvotes: 9

Supriya
Supriya

Reputation: 581

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

Upvotes: 0

Khairulnizam Hasan
Khairulnizam Hasan

Reputation: 16

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

Upvotes: 0

Khurshid Ansari
Khurshid Ansari

Reputation: 5075

Simple:

Replace the requireCordovaModule to require :

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

Upvotes: 16

Iheb Miled
Iheb Miled

Reputation: 19

remove cordova-plugin-crosswalk-webview

Upvotes: 0

Dominic
Dominic

Reputation: 4952

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.

Upvotes: 8

mattdiep
mattdiep

Reputation: 106

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

Upvotes: 9

Related Questions