如何在发布软件包时避免重复安装 React?

huangapple go评论58阅读模式
英文:

How do I avoid double react installations when publishing packages?

问题

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.

英文:

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.

答案1

得分: 1

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

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.

huangapple
  • 本文由 发表于 2023年2月7日 05:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定