cordova-cli icon indicating copy to clipboard operation
cordova-cli copied to clipboard

--nosave flag causes errors when adding platforms/plugins

Open asgoth opened this issue 6 years ago • 4 comments

Bug Report

Problem

On several cordova commands, the nosave flag causes issues.


What is expected to happen?

Platform/plugin correctly installed. Installation succeeds without the nosave flag.

What does actually happen?

On cordova platform add ios --nosave:

~/Development/git/test-cordova/test » cordova platform add ios --nosave
Using cordova-fetch for cordova-ios@^5.0.0
Adding ios project...
Creating Cordova project for the iOS platform:
	Path: platforms/ios
	Package: io.cordova.hellocordova
	Name: HelloCordova
iOS project created with [email protected]
Discovered plugin "cordova-plugin-whitelist" in config.xml. Adding it to the project
Installing "cordova-plugin-whitelist" for ios
Failed to install 'cordova-plugin-whitelist': Error: Cannot find module './src/superspawn'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at Object.get (/Users/xxx/Development/git/test-cordova/test/node_modules/cordova-common/src/util/addProperty.js:27:44)
    at Object.<anonymous> (/Users/xxx/Development/git/test-cordova/test/platforms/ios/cordova/lib/Podfile.js:26:43)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
Failed to restore plugin "cordova-plugin-whitelist" from config.xml. You might need to try adding it again. Error: Error: Cannot find module './src/superspawn'

On cordova plugin add xxx --nosave (doesn't matter which plugin):

~/Development/git/test-cordova/test » cordova plugin add cordova-plugin-camera --nosave
Installing "cordova-plugin-camera" for android
cordova-android version check failed ("/Users/xxx/Development/git/test-cordova/test/platforms/android/cordova/version"), continuing anyways.
Unable to load PlatformApi from platform. Error: Cannot find module 'properties-parser'
Failed to install 'cordova-plugin-camera': 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:178:17)
    at CordovaEventEmitter.emit (/Users/xxx/.nvm/versions/node/v10.15.3/lib/node_modules/cordova/node_modules/cordova-common/src/events.js:69:22)
    at Object.getPlatformApiFunction (/Users/xxx/.nvm/versions/node/v10.15.3/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/util.js:371:20)
    at Object.getPlatformApi (/Users/xxx/.nvm/versions/node/v10.15.3/lib/node_modules/cordova/node_modules/cordova-lib/src/platforms/platforms.js:55:32)
    at handleInstall (/Users/xxx/.nvm/versions/node/v10.15.3/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:580:29)
    at /Users/xxx/.nvm/versions/node/v10.15.3/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:349:28
    at process._tickCallback (internal/process/next_tick.js:68:7)
Unhandled error. (The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.)

Information

Command or Code

cordova platform add ios android --nosave
cordova plugin add cordova-plugin-camera --nosave

Environment, Platform, Device

Some commands fail for ios, other for android

Version information

  • osx 10.14.4
  • npm 6.4.1
  • node 10.15.3
  • cordova (cli) 9.0.0

Checklist

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

asgoth avatar Apr 01 '19 12:04 asgoth

Thank you for creating this issue @asgoth . Actually I noticed this bug myself just a short while ago but forgot to create an issue.

Then, I also did some research on what exactly is broken. In short: some versions of npm automatically prune installed packages during installation (https://github.com/npm/npm/issues/16853). This means that if you run npm i --no-save A followed by npm i --no-save B you have no guarantee to still have A or any of its dependencies installed. Since we use npm to install packages, this completely breaks our --no-save option. This problem also seems to have bitten Cordova users before: https://github.com/npm/npm/issues/16853#issuecomment-368287473. However, I cannot confirm that recent npm versions have reliably fixed the pruning issue.

My preferred course of action would be to just remove our --no-save option altogether. Firstly, because I do not know how we could fix this and secondly because it does not play well with Cordova workflows that I would recommend. But since you were using it, would you care to share your use-case for this option?

raphinesse avatar Apr 01 '19 12:04 raphinesse

Hi @raphinesse,

We are developing Angular libraries. Some of the components integrate with cordova. For that we made a demo (cordova) application, which we remove and scaffold again when a plugin needs to be added. At the time, didn't want to pollute that project's package.json with cordova related stuff. But maybe I need to revise that.

So not a big issue.

asgoth avatar Apr 01 '19 13:04 asgoth

@asgoth If you're using Cordova 9 (with Cordova-Lib >= 9.0.1) then you can include those Cordova related things under your package's devDependencies :slightly_smiling_face:

dpogue avatar Apr 17 '19 03:04 dpogue

What worked for me was adding the package cordova-common. If you have a mono-repo with multiple apps, add that package in all the apps, not in the root package.json.

SebasWebDev avatar Mar 23 '20 07:03 SebasWebDev