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

Add legacyPeerDeps option to NodePackageInstallTask

Open KirillMetrik opened this issue 4 years ago β€’ 14 comments

πŸš€ Feature request

Description

Right now there is an opportunity to use NodePackageInstallTask when authoring schematics. This works well but there are cases when running simple npm install is not enough. For example, last version of NPM might throw "Unable to resolve dependency tree" error. This can be easily fixed with an --legacy-peer-deps option. Unfortunately, this option is not available for NodePackageInstallTask.

Describe the solution you'd like

Add ability to provide legacyPeerDeps option to NodePackageInstallTask. Even better solution would be to add opportunity to provide any custom options to NodePackageInstallTask. They should be passed down to command line automatically.

Describe alternatives you've considered

I've tried to write my own NodePackageInstallTask with an extra option. But this didn't work. I've discovered that provided options are just meta-data for the executor (https://github.dev/angular/angular-cli/blob/366cabc66c3dd836e2fdfea8dad6c4c7c2096b1d/packages/angular_devkit/schematics/tasks/package-manager/executor.ts#L112). From executor code it is clear that there is a predefined set of options for NPM task and there is no way to override or extend this.

KirillMetrik avatar Mar 23 '22 17:03 KirillMetrik

The force option can be used in this case which bypasses the peer dependencies conflicts.

Isn’t this enough for you?

alan-agius4 avatar Mar 23 '22 18:03 alan-agius4

@alan-agius4 i haven't found this option neither. Maybe I am looking wrongly. Could you give an example of it's usage? This is where I am looking: https://github.dev/angular/angular-cli/blob/366cabc66c3dd836e2fdfea8dad6c4c7c2096b1d/packages/angular_devkit/schematics/tasks/package-manager/install-task.ts#L43

KirillMetrik avatar Mar 23 '22 19:03 KirillMetrik

I did take a look at this and it appears that the force option is part of the NodeWorkflow which is only set when using ng update --force

Outside of ng update, we never really needed to run npm install with --legacy-peer-deps and I guess that better option would be to solve the peer dependency errors in the first place.

alan-agius4 avatar Mar 24 '22 15:03 alan-agius4

@alan-agius4 and do you please have an example of calling NodePackageInstallTask from schematics code with --force enabled?

KirillMetrik avatar Mar 25 '22 14:03 KirillMetrik

That is not possible, since the force option is strictly controlled by the Angular CLI. We only allow using the force option in ng update.

alan-agius4 avatar Mar 25 '22 14:03 alan-agius4

I have a similar need. Today, at the company I work for, we have a CLI application that creates angular projects and configures them with the company needs: internal packages, specific application building examples, code templates, unit testing with jest, etc. To do that, we consume the schematic ng-new and then apply our schematics; we also use the NodePackageInstallTask, which eventually breaks at the current state/version of the code if a developer is using npm7 or npm8. Adding a legacyPeerDeps option would help us a lot, as the alternative would be a spawn npm i --legacy-peer-deps outside the schematics flow.

I was studying the code and it wouldn't be a difficult PR if needed.

mzocateli avatar Apr 14 '22 18:04 mzocateli

@mzocateli, why not amend the custom ng-new dependencies to not require legacy-peer-deps in the first place?

In general, using legacy-peer-deps is not recommended.

alan-agius4 avatar Apr 14 '22 18:04 alan-agius4

Indeed, I agree, it would be the ideal scenario. But as it is a large company with a technology area with more than 20k people (even though it is not a technology company) and producing a range of internal packages, we are unable to control the peer dependencies of all these packages that eventually enter the distributed templates in our application. Some of them are truly legacy packages despite modernization initiatives.

I know this isn't exactly angular's problem, but making this kind of coexistence easier would help a lot πŸ˜…

mzocateli avatar Apr 14 '22 18:04 mzocateli

I fell like it would be better to alter the custom schematic to add an .npmrc file in the project root to configure legacy-peer-deps, this would also effect npm install when executed outside of schematics.

alan-agius4 avatar Apr 14 '22 19:04 alan-agius4

A small update from me. In the end I've done significant efforts to resolve all peer dependencies conflicts, so node installation task works now. But I still feel that it would be a great feature to allow to pass any options to npm installation task (not just legacyPeerDeps).

KirillMetrik avatar Apr 14 '22 20:04 KirillMetrik

@KirillMetrik, the Install task is packager manager agnostic.

I feel that if you need a to provide a specific option, likely you also need this when running NPM directly, and therefore in that case using a workspace level npm config file would be a better approach.

alan-agius4 avatar Apr 14 '22 21:04 alan-agius4

I fell like it would be better to alter the custom schematic to add an .npmrc file in the project root to configure legacy-peer-deps, this would also effect npm install when executed outside of schematics.

It worked, at least for Angular 12+ projects using npm8, but I think that'll do it.

mzocateli avatar Apr 14 '22 22:04 mzocateli

Closing, as using legacyPeerDeps is not recommend.

In the case you need to use it, it would be better to configure this through an npmrc file, so that when running npm directly this setting is also applied.

Let’s reconsider this in the future if there is a bigger demand for this.

alan-agius4 avatar Apr 17 '22 16:04 alan-agius4

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.