cordova-cli: Unable to "prepare" some projects [9.0.0]

Bug Report

Problem

Running cordova prepare inside a stub project does not create the project correctly.

What is expected to happen?

For a subsequent call to cordova build android to produce an APK.

What does actually happen?

cordova build android will fail with:

Unable to load PlatformApi from platform. Error: Cannot find module 'q'
Unhandled error. (The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.)

Information

Some of our projects are not restoring correctly when I run “cordova prepare” on them, despite prepare not reporting failure. After some investigation it appears that the root ‘node_modules’ folder is effectively empty (only contains the cordova plugins, not any other dependencies), causing subsequent cordova commands to fail. I haven’t been able to figure out what factor causes the failure as some complex projects seem unaffected but a similar minimal example project seems to trigger the issue. This isn’t an issue on versions of cordova prior to 9.0.0.

Command or Code

  1. Unzip example project stub_project.zip
  2. Navigate to project in terminal
  3. Run the following commands
cordova prepare --verbose
No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are different. Updating package.json with most current list of platforms.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
Discovered platform "android" in config.xml or package.json. Adding it to the project
No scripts found for hook "before_platform_add".
No version supplied. Retrieving version from config.xml...
Grabbing pinned version.
Using cordova-fetch for cordova-android@^8.0.0
fetch: Installing cordova-android@^8.0.0 to [...]\cordova_9_testcase
Running command: npm install cordova-android@^8.0.0 --production --no-save
Command finished with error code 0: npm install,cordova-android@^8.0.0,--production,--no-save
Removing "cordova-" prefix from cordova-android
Adding android project...
PlatformApi successfully found for platform android
Creating Cordova project for the Android platform:
        Path: platforms\android
        Package: com.test.project
        Name: Test_project
        Activity: MainActivity
        Android target: android-28
Copying android template project to platforms\android
Subproject Path: CordovaLib
Subproject Path: app
Android project created with cordova-android@8.0.0
Saving android@8.0.0 into platforms.json
No scripts found for hook "after_platform_add".
PlatformApi successfully found for platform android
Checking for saved plugins that haven't been added to the project
Plugin 'cordova-plugin-whitelist' found in config.xml... Migrating it to package.json
Discovered saved plugin "cordova-plugin-whitelist". Adding it to the project
No scripts found for hook "before_plugin_add".
No version specified for cordova-plugin-whitelist, retrieving version from config.xml
No version for cordova-plugin-whitelist saved in config.xml or package.json
Attempting to use npm info for cordova-plugin-whitelist to choose a compatible release
Running command: npm view cordova-plugin-whitelist --json
Command finished with error code 0: npm view,cordova-plugin-whitelist,--json
Running command: [...]\cordova_9_testcase\platforms\android\cordova\version
Command finished with error code 0: [...]\cordova_9_testcase\platforms\android\cordova\version
Ignoring invalid version in cordova-plugin-whitelist cordovaDependencies: 2.0.0 (must be a single version <= latest or an upper bound)
Calling plugman.fetch on plugin "cordova-plugin-whitelist@1.3.3"
fetch: Installing cordova-plugin-whitelist@1.3.3 to [...]\cordova_9_testcase
Running command: npm install cordova-plugin-whitelist@1.3.3 --no-save
Command finished with error code 0: npm install,cordova-plugin-whitelist@1.3.3,--no-save
Copying plugin "[...]\cordova_9_testcase\node_modules\cordova-plugin-whitelist" => "[...]\cordova_9_testcase\plugins\cordova-plugin-whitelist"
Calling plugman.install on plugin "[...]\cordova_9_testcase\plugins\cordova-plugin-whitelist" for platform "android
Installing "cordova-plugin-whitelist" for android
Running command: [...]\cordova_9_testcase\platforms\android\cordova\version
Command finished with error code 0: [...]\cordova_9_testcase\platforms\android\cordova\version
Running command: [...]\cordova_9_testcase\platforms\android\cordova\version
Command finished with error code 0: [...]\cordova_9_testcase\platforms\android\cordova\version
Finding scripts for "before_plugin_install" hook from plugin cordova-plugin-whitelist on android platform only.
No scripts found for hook "before_plugin_install".
Install start for "cordova-plugin-whitelist" on android.
Beginning processing of action stack for android project...
Action stack processing complete.
Install complete for cordova-plugin-whitelist on android.
           This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Finding scripts for “after_plugin_install” hook from plugin cordova-plugin-whitelist on android platform only. No scripts found for hook “after_plugin_install”. No scripts found for hook “after_plugin_add”. Checking for any plugins added to the project that have not been installed in android platform No differences found between plugins added to project and installed in android platform. Continuing… Cannot find module ‘./src/ConfigParser/ConfigParser’ Error: Cannot find module ‘./src/ConfigParser/ConfigParser’ at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) at Function.Module._load (internal/modules/cjs/loader.js:506:25) at Module.require (internal/modules/cjs/loader.js:636:17) at Object.get ([…]\cordova_9_testcase\node_modules\cordova-common\src\util\addProperty.js:27:44) at Object.<anonymous> ([…]\cordova_9_testcase\platforms\android\cordova\lib\prepare.js:30:45) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions…js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3)

cordova build android --verbose
No scripts found for hook "before_build".
No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are the same. No pkg.json modification.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
Unable to load PlatformApi from platform. Error: Cannot find module 'q'
Unhandled error. (The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.)
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.)
    at CordovaEventEmitter.emit (events.js:171:17)
    at CordovaEventEmitter.emit ([...]\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-common\src\events.js:69:22)
    at Object.getPlatformApiFunction ([...]\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\util.js:384:20)
    at Object.getPlatformApi ([...]\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\platforms\platforms.js:55:32)
    at[...]\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\prepare.js:52:38
    at Array.map (<anonymous>)
    at [...]\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\prepare.js:50:47
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)

Resulting project: resulting_project.zip

If you repeat this, but remove the line in config.xml that refers to cordova-plugin-whitelist then the project will restore correctly.

Environment, Platform, Device

Window 10 - Command prompt Also occurring on Ubuntu server

Version information

cordova -v
9.0.0 (cordova-lib@9.0.1)
node -v
v10.13.0

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@Lindsay-Needs-Sleep there’s a different situation which it happens, even with an existing package.json. I’m in the process of moving to using local packages, specified via the package.json and this issue seems to be happening every time with that change. I’m trying to figure out the magic combination which will allow that to work… But had to move onto other work.

@breautek thanks for mentioning that NPM flag, it might come in handy. It might be the NPM default, but not the cordova. Either way the issue was more that adding a platform would modify the revision of existing dependencies, which is unhelpful if your expecting your server to produce predictable builds.

but it looks like the version numbers in the package.json are being purposefully modified by cordova during the cordova platform add android to include the “^” character.

This is the NPM default. You can edit your npm config to change this, or add an .npmrc file to configure npm specifically for your cordova project. You can change this by modifying the save-prefix config.

https://docs.npmjs.com/misc/config#save-prefix