Cordova error: Using "requireCordovaModule" to load non-cordova module "q" is not supported
CordovaCordova PluginsCordova Problem Overview
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.
Cordova Solutions
Solution 1 - Cordova
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
Solution 2 - Cordova
Simple:
Replace the requireCordovaModule to require :
requireCordovaModule("q") to require("q")
Solution 3 - Cordova
i can solve this issue by downgrading cordova to 8.1.1
.
npm remove cordova -g && npm install -g cordova@8.1.1
Solution 4 - Cordova
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.
Solution 5 - Cordova
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
Solution 6 - Cordova
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.
Solution 7 - Cordova
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
Solution 8 - Cordova
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
- Update Cordova:
npm install -g cordova
- Update SDK, ex:
./android/tools/bin/sdkmanager "build-tools;28.0.3" "platforms;android-28"
- 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
- Remove node_modules, platforms and plugins folders
- Remove package.json and package-lock.json
- Remove from config.xml plugins/platforms/engine
- Add your platform, ex:
cordova platform add android
- Add your plugins updated, ex:
cordova plugin add cordova-plugin-whitelist
- ex:
npm install
- 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 :)
Solution 9 - Cordova
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@8.1.1
Solution 10 - Cordova
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;
Solution 11 - Cordova
remove cordova-plugin-crosswalk-webview
Solution 12 - Cordova
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
Solution 13 - Cordova
C:\Windows\System32 to my PATH variable to fix this odd issue.
Solution 14 - Cordova
Here's a little tip in finding where requireCordovaModule
exists:
For those looking to change requireCordovaModule
to require
, I have found it easy to search for the requireCordovaModule
text right from the command line by using this command:
grep -r 'requireCordovaModule' ./
Then, you can drill into those files and rename the requireCordovaModule
to require
.
Solution 15 - Cordova
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