-1
  • I have a react application say project-A which I need to publish as a npm package and this project have several dependencies such as material-ui, dompurify, recharts etc.

  • Now in other application say Project-B I need to install Project-A as dependency now I want all the dependencies required by Project-A to be automatically installed in Project-B. As I don't want Project-B to install all the required dependencies manually.

  • I have copied all dependencies in peerDependencies section of package.json in Project-A and I am expecting these dependencies to be installed as soon as we do npm install Project-A in Project-B terminal

  • Previously was trying with npm version 6.14.13, it only warns to install peer dependencies manually

  • Some online solutions suggested to upgrade npm version, after upgrading the same it started throwing error.

  • current node version - 18.14.2, current npm version - 9.5.0

Getting below error with upgraded node and npm versions-

enter image description here

Tried solutions:

  • Tried using command as suggested by some online solutions npm install Project-A --legacy-peer-deps but it ignores peer dependencies and does not install them.
  • Some solutions also suggesting to use npm install --force it doesn't throw error during npm install but doesn't install peer dependencies as well.

Everyone is suggesting to use commands to ignore peer dependencies then what is the use of upgrading the versions of npm and node because this was already happening in older versions and we had to manually install peer dependencies?

Can someone please suggest in which npm and node versions peer dependencies can actually be installed automatically without having to ignore them using commands or is there any other solution to fix this error?

neha singh
  • 131
  • 1
  • 1
  • 7
  • npm 7 and above will actually attempt to do so but might fail due to conflicting or incompatible peer dependency requirements. ultimately you are gonna have to either manually install them or upgrade/fix the error with conflicting dependencies – Zein Feb 27 '23 at 11:07

1 Answers1

1

Peer dependencies indicate dependencies that have to be explicitly added by the code base that consumes the dependency.

If you want dependencies of Project-A to be automatically installed when adding Project-A as a dependency in Project-B, they have to be regular dependencies of Project-A, not peer dependencies.

In somewhat simplified terms, peer dependencies are useful if you have a Project-A and a Project-B to be consumed by a Project-C. Project-A and B both share a dependency and declare it as a peer dependency for Project-C to include, to make sure two versions of the same are not pulled in by A and B into C.

martinstark
  • 187
  • 8
  • Hello Martin, in this case if I have all the required dependencies within regular dependencies section of project-A including react and react-dom, then I need not install any dependency manually in Project-B even react and react-dom? – neha singh Feb 27 '23 at 11:13
  • @nehasingh as long as the dependencies are baked into the Project A bundle when it is transpiled. E.g. the dependencies (react and react-dom) cannot be declared as "externals" if using webpack to bundle Project A. If both project A and B are react projects, I would recommend marking react and react-dom as peer dependencies and external modules of Project A, and declare them as regular dependencies in project B, so as to not get duplicate dependencies in the final bundle. – martinstark Feb 27 '23 at 11:15
  • Thanks, will give it a try as per your suggestions. – neha singh Feb 27 '23 at 12:15