Home >Web Front-end >JS Tutorial >What does `npm install --legacy-peer-deps` do and when is it recommended?

What does `npm install --legacy-peer-deps` do and when is it recommended?

Linda Hamilton
Linda HamiltonOriginal
2024-11-28 14:15:19991browse

What does `npm install --legacy-peer-deps` do and when is it recommended?

What does npm install --legacy-peer-deps do exactly? When is it recommended / What's a potential use case?

TL;DR:

  • NPM v7 installs peer dependencies by default; this is not the case with previous versions of NPM.
  • NPM modules must name specific versions of their peer dependencies.
  • If you already have a peer dependency installed, but not with a version named by the module, then NPM v7 will throw an error.
  • Adding --legacy-peer-deps ignores this new requirement, at the risk of introducing breaking changes.

--legacy-peer-deps restores peer dependency installation behavior from NPM v4 thru v6

One way of thinking of this flag is that it isn't doing something new; rather it's telling NPM not to do something new, since NPM v7 now installs peer dependencies by default.

In many cases, this is leading to version conflicts, which will break the installation process.

The --legacy-peer-deps flag was introduced with v7 as a way to bypass peer dependency auto-installation; it tells NPM to ignore peer deps and proceed with the installation anyway. This is how things used to be with NPM v4 thru v6.

Dependencies vs peerDependencies

If you're unclear about the difference between regular deps and peer deps, here is a bit of context:

  • Dependencies: Libraries or modules that an NPM module needs in order to work in production.
  • peerDependencies: A peer dependency is a specific version or set of versions of a third-party software library that a module is designed to work with.

This issue is being driven, in part, by React v17

Due to the large number of modules that haven't specifically added React v17 (or more recently, React 18) as a peer dependency, it's now commonplace to encounter the unable to resolve dependency tree error when running npm installs within a v17 React application.

This error will fire whenever a module (or any of its own dependencies) lists a previous major version of React as a peer dependency without specifically including React v17 as well.

How to check peer dependencies for any given module

NPM itself doesn't list peer deps on the pages of a given module. However, there is a simple workaround to check for peer deps, either before or after install. Simply run:

npm info name-of-module peerDependencies

This command will return the name of each peer dependency along with all compatible version(s).

The above is the detailed content of What does `npm install --legacy-peer-deps` do and when is it recommended?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn