Search code examples
node.jsreactjsnpmnpm-installpeer-dependencies

npm - need to automatically install peer dependencies in parent project without ignoring them


  • 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?


Solution

  • 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.