apache / cordova-lib

Apache Cordova Tooling Library
https://cordova.apache.org/
Apache License 2.0
221 stars 242 forks source link

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

Open ath0mas opened 3 years ago

ath0mas commented 3 years ago

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 _/nodemodules/ 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 _/nodemodules/ and runs fine for

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 _/nodemodules/ and so possibly failing project creation (like when a plugin _afterprepare 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 _/nodemodules/ 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.

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

raphinesse commented 2 years ago

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 });
ath0mas commented 2 years ago

@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