Dependencies are wiped on each step of project creation with cordova-lib and NPM 7
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
- OS: Windows 10
- Node: 16.0.0 / 14.6.1
- npm: 7.10.0 / 6.14.12
- Cordova:
- CLI 10.0.0
- Lib 10.0.0
- Common 4.0.2
- Fetch 3.0.1
- Cordova Plugins:
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
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 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