Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies are wiped on each step of project creation with cordova-lib and NPM 7 #868

Open
3 tasks done
ath0mas opened this issue Apr 25, 2021 · 3 comments
Open
3 tasks done
Labels
info-needed Further information is requested

Comments

@ath0mas
Copy link
Contributor

ath0mas commented Apr 25, 2021

Bug Report

Problem

What is expected to happen?

For a cordova project created through cordova-lib node api, with npm@7,
we expect the npm packages required by each cordova step/command to be stored and cumulated in /node_modules/
like what's done using npm@6 or with cordova-cli itself with npm@6 and npm@7.

What does actually happen?

Project creation has correct /node_modules/ and runs fine for

  • cordova-lib with npm@6 only 😢
  • cordova-cli with npm@6 and npm@7 👍

In those 3 cases, the order to add plugins and platform does not matter: plugins first and platform after them, or the opposite, platform first and then plugins.

😢 Using cordova-lib with npm@7 results in incomplete /node_modules/ and so possibly failing project creation (like when a plugin after_prepare hook script called during cordova platform add does not find one of its own dependencies).

Information

Again only since Npm 7, each step is fetching its packages correctly inside /node_modules/ but it is also like wiping its content first.
Each call to await cordova.plugin('add', pluginName); or await cordova.platform('add', platformName); will result in its content to have only new deps for current specific plugin or platform.

  • add first plugin: its deps are in /node_modules/
  • add second plugin: its deps are in /node_modules/ but not those of first plugin (should be, like with cli and npm 6)
  • add platform: its deps are in /node_modules/ but not those of the two plugins (should be, like with cli and npm 6)
    • each plugin is now installed for this platform, also calling the after_prepare hook script if present
    • ERROR here if script logically uses one of its own dependencies

Command or Code

(simplified version of my code ; I am working on a basic and easily reusable project to share here soon)

await cordova.plugin('add', 'cordova-plugin-device');
await cordova.plugin('add', 'cordova-plugin-androidx-adapter');
await cordova.platform('add', 'android');

will give such failure

.. // "cordova-plugin-device" added first and ok
.. // "cordova-plugin-androidx-adapter" added second and ok
...
.. // starting _plaftom add_ for "android"
Installing "cordova-plugin-androidx-adapter" for android
Installing "cordova-plugin-device" for android
cordova-plugin-androidx-adapter: EXCEPTION: Failed to load dependencies: Error: Cannot find module 'recursive-readdir'
Require stack:
- C:\cdv-lib\helloCdvLib\plugins\cordova-plugin-androidx-adapter\apply.js
- C:\cdv-lib\node_modules\cordova-lib\src\hooks\HooksRunner.js
- C:\cdv-lib\node_modules\cordova-lib\src\plugman\install.js
- C:\cdv-lib\node_modules\cordova-lib\src\plugman\plugman.js
- C:\cdv-lib\node_modules\cordova-lib\cordova-lib.js
- C:\cdv-lib\node_modules\cordova\cordova.js
- C:\cdv-lib\generateHelloCdvLib.js

Environment, Platform, Device

Version information

Checklist

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

Could you please check if passing the option save: true solves your issue?

await cordova.plugin('add', 'cordova-plugin-device', { save: true });
await cordova.plugin('add', 'cordova-plugin-androidx-adapter', { save: true });
await cordova.platform('add', 'android', { save: true });

@raphinesse raphinesse added the info-needed Further information is requested label Jun 23, 2021
@ath0mas
Copy link
Contributor Author

ath0mas commented Jul 31, 2021

@raphinesse adding the option save: true solves the issue !! 🎉

When using npm 6 I don't see any difference between with and without this option (mostly for dependencies fetched and stored), while using npm 7 it seems needed to add it 👍

Indeed this option seems to be set to true using cli without options, and mostly none of --nosave or --save : cli.js#L447

@nachocodoner
Copy link

I think this issue affects on a use-case when using Meteor 3 and cordova integration, Meteor 3 has built-in Node with a newer version of NPM. When installing a specific plugin with <dependency> tag the issue mentioned here triggers and node_modules deps are lost.

As part of fixing this issue, I am trying to use the save option as suggested here. However, I get this error:

=> Errors executing Cordova commands:
  While adding platform Android to Cordova project:
  Cordova error: CordovaError: Failed to fetch platform cordova-android
  Probably this is either a connection problem, or platform spec is incorrect.
  Check your connection and platform name/version/URL.
  CordovaError: Error: Cannot find module 'cordova-android/package.json'

Do you know what could be the cause?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants