问题
When I run npm i on my current react project, I get the following warning regarding react peerDependency:
npm WARN react-tap-event-plugin@3.0.3 requires a peer of react@^16.0.0-0 < 16.4.0 but none is installed. You must install peer dependencies yourself.
npm WARN react-paginate@4.4.4 requires a peer of react@^15.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN formsy-react@0.19.5 requires a peer of react@^0.14.0 || ^15.0.0 but none is installed. You must install peer dependencies yourself.
While in my package.json, I am using latest version of react:
"react": "^16.7.0"
I am new to node and npm. I would like to know what is the good practice for installing npm peerDependencies:
1.) Can the warnings for lower versions be ignored if updated version is already specified in package.json.
2.) As per https://lexi-lambda.github.io/blog/2016/08/24/understanding-the-npm-dependency-model/ and https://github.com/npm/npm/issues/6565
npm provides dependency isolation and peerDepencies needs to be manually installed, so should I install all 3 versions of react but I fear that will break the import statements.
3.) If none of the above two, which version should I use in package.json. P.S. there are many more dependencies in my package.json which might require latest version also.
回答1:
Taking Danyal's answer further, you can upgrade formsy-react and remove react-tap-event-plugin:
- Update formsy-react to latest version: (1.1.5 at time of writing), the latest version of this package supports react ^16.
- react-tap-event-plugin supports react version upto version 16.4. You have a few options here:
- Downgrade react: downgrading to 16.4 will remove all the warnings, but will restrict your ability to upgrade in the future
- Remove react-tap-event-plugin: According to the documentation https://www.npmjs.com/package/react-tap-event-plugin. This module is actually deprecated thanks to fixes made to later browsers. Check the blog post for info.
- Fork react-tap-event-plugin: I wouldn't do this myself, but you could fork the plugin and publish it yourself with the updated react peerDependency.
回答2:
A peer dependency means that a package is applicable to used with a particular version of the dependency & wouldn't work as intended if you exceed the specified version.
In your case react-tap-event-plugin@3.0.3 requires a version of React less than 16.4.0, react-paginate@4.4.4 requires any version of React 15 and the same for formsy-react@0.19.5.
You would need to downgrade from React 16.7.0, but that can break your application if you are using 16.7.0 features, or you could remove the packages and use another one or write the package's logic from start yourself.
Tip: always make sure to read package dependencies on npm website before actually considering to use a package for your project.
来源:https://stackoverflow.com/questions/54288023/multiple-versions-of-same-peerdependency-required