1

I've developed with React for a long time but I've only recently tried my hand at publishing packages.

A dependency of the package I'm working on causes issues if there are conflicting React installations between the package and the project it's being installed into. (The package is react-query)

How do I handle this situation?

Ideally I'd like the two versions to be the same as React 17.x and React 18.x have weird type changes which causes issues when being used together. But I'm honestly completely lost.

Googling doesn't seem to bring up anything I can use.

lpetrucci
  • 1,285
  • 4
  • 22
  • 40
  • Either downgrade your react version or upgrade react-query – Konrad Feb 06 '23 at 21:12
  • sorry, did not intended to close question for good. just was looking for existing answers. until I misunderstood conditions, you don't want to declare `React` in `dependencies` but in `peerDependencies` instead. And define largest range possible. Then project, which installs your package, will install its own React version. If it does not help - may you add more details? – skyboyer Feb 06 '23 at 21:15

1 Answers1

1

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

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

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

  • If these include react matching the version requirements, then nothing else needs to be done
  • If a suitable version of react was not found, then npm will install the latest matching version

Behavior can be different with older versions of npm and only a warning will be printed in the console during npm install.

Valentin
  • 10,769
  • 2
  • 17
  • 27