Analysis of peerDependencies WARNING problem

peerDependenciesIt is an early concept of npm, which has not been noticed before, because it is mainly related to the development of plug-ins. It was not until recently that I was responsible for doing some plug-in development.

We are dependenciesand devDependenciesshould be very familiar, but in addition to their two outside, package.json where you can configure a call peerDependenciesconfiguration.

So peerDependencieswhat does this configuration do? Here we give a small example:

Suppose our current project is MyProject, there are some dependencies in the project, for example, there is a dependency package PackageA, the package.json file of the package specifies the dependency on PackageB:

{
    "dependencies": {
        "PackageB": "1.0.0"
    }
}

If we execute npm install PackageA in our MyProject project, we will find that the directory structure of our project will be as follows:

MyProject
|- node_modules
   |- PackageA
      |- node_modules
         |- PackageB

Then in our project, we can introduce "PackageA" through the following statement:

var packageA = require('PackageA')

However, if you want to directly reference PackageB in your project:

var packageA = require('PackageA')
var packageB = require('PackageB')

This will not work, even if PackageB has been installed. Because Node will only look for PackageB in the "MyProject / node_modules" directory, it will not look for node_modules under the PackageA module.

Therefore, in order to solve this problem, in the MyProject project package.json we must directly declare the dependency on PackageB and install it.

However, sometimes we can directly quote without declaring a dependency on PackageB in the current project.

Why can it be used directly in the project without declaration? 这就是peerDependencies的作用了.

For example, the Package.json package.json file above is as follows:

{
    "peerDependencies": {
        "PackageB": "1.0.0"
    }
}

Then, it will tell npm: if a package lists me as a dependency, then that package must also have a dependency on PackageB.

In other words, if you npm install PackageA, you will get the following directory structure:

MyProject
|- node_modules
   |- PackageA
   |- PackageB

Therefore, you may notice that in npm2, even if the current project MyProject does not directly depend on PackageB, the PackageB package will still be installed in the node_modules folder of the current project.

In npm2, the dependencies specified by peerDependencies in the PackageA package will be forcibly installed along with npm install PackageA, so there is no need to specify a dependency on the contents of peerDependencies in PackageA in the package.json file of the host environment.

但是在npm3中,peerDependencies的表现与npm2不同

npm3 will no longer require the dependency package specified by peerDependencies to be forcibly installed. On the contrary, npm3 will check whether the installation is correct after the installation is completed. If it is incorrect, it will print a warning prompt to the user.

Take the above example. If we install PackageA with npm install PackageA, you will get a warning saying:

PackageB是一个需要的依赖,但是没有被安装。 

Insert picture description here
At this time, you need to manually specify PackageB's dependencies in the MyProject project's package.json file.

Reference link: Introduction and analysis of peerDependencies
Reference link: PeerDependencies Reference link: Explore peerDependencies
of npm dependency management

Published 398 original articles · Liked 182 · Visits 370,000+

Guess you like

Origin blog.csdn.net/yexudengzhidao/article/details/105542562