search

Home  >  Q&A  >  body text

How to avoid duplicate installation of React during package release?

I've been developing with React for a long time, but only recently tried publishing packages.

One of the dependencies of a package I'm developing is causing problems with React installations where there's a conflict between the package and the project it's being installed into. (The package is react-query)

How should I handle this situation?

Ideally I would like the two versions to be consistent, as React 17.x and React 18.x have weird type changes that cause problems when they are used together. But honestly, I'm completely lost.

Searching doesn't seem to turn up anything I can use.

P粉036800074P粉036800074453 days ago586

reply all(1)I'll reply

  • P粉105971514

    P粉1059715142023-09-09 07:17:07

    You should specify react as peer dependency in your lib package.json file:

    "peerDependencies": {
      "react": ">= 17"
    }
    

    When encountering a peer dependency, npm will check the dependencies of the project using your lib:

    • If these dependencies include react that match the version requirements, no other operations are required
    • If no suitable version of react is found, npm will install the latest matching version

    In older versions of npm the behavior may be different and only warnings will be printed in the console during npm install.

    reply
    0
  • Cancelreply