英文:
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, thennpm
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论