Unmet peer dependencies error with @tanstack/query-persist-client-core #4885
Describe the bug#
Hello,
Installing @tanstack/query-persist-client-core, @tanstack/query-sync-storage-persister and @tanstack/query-async-storage-persister is leading to unmet peer dependencies errors using Yarn v3:
➤ YN0002: │ @tanstack/query-async-storage-persister@npm:4.22.4 doesn't provide @tanstack/query-core (p61c1a), requested by @tanstack/query-persist-client-core
➤ YN0002: │ @tanstack/query-sync-storage-persister@npm:4.22.4 doesn't provide @tanstack/query-core (pbe790), requested by @tanstack/query-persist-client-core
➤ YN0002: │ @tanstack/react-query-persist-client@npm:4.23.0 [57297] doesn't provide @tanstack/query-core (pd24d6), requested by @tanstack/query-persist-client-core
This is caused by @tanstack/query-persist-client-core requesting @tanstack/query-core as a peer dependencies which is not provided in the previously mentioned packages either as a dependency or passing it down as a peerDependency
My first thought would be to set @tanstack/query-core as a dependency of @tanstack/query-persist-client-core but I'm not sure this is the strategy you want to follow
Your minimal, reproducible example#
https://stackblitz.com/edit/node-yjkne1?file=pnpm-lock.yaml
Steps to reproduce#
- Run
pnpm ioryarn installin console
Expected behavior#
No unmet peer dependencies warnings or errors should be displayed
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: MacOs
- Yarn Version: 3.3.1
TanStack Query version#
4.23.0
TypeScript version#
No response
Additional context#
No response
TkDodo added labelsbug
TkDodo added labelspackage: react-query-persist-client
TkDodo removed labelspackage: react-query-persist-client
TkDodo added labelspackage: query-persist-client-core
If we add it as a dependency instead of a peerDependency, wouldn't this mean that it would be shipped as a copy with the plugin?
It used to be a valid issue but package manager have evolved and are more smart about this. This issue is fixed by what is called hoisting which is now implemented by most package managers and then bundlers should do the work to prevent duplicating dependencies in the final build.
Seems like I don't really understand what it means to have a dependency on something 😅 .
To be fair the difference between a dependency and a peerDependency is not all that clear for me either. In your case I'm pretty sure there is definitely multiple valid dependency/peerDependency trees to achieve the same result. I believe it comes down to you to choose the "UX" you want about installing your packages.
What I'm sure of is that:
- Given a dependency
Awhich has peerDependenciesAp1, Ap2 ..., ApN - If you add
Ato one of your packagePas a dependency you have to choose for eachApXbetween adding it toP's dependencies or peerDependencies - If you don't do that you might have bugs because you don't have the correct version of
@tanstack/query-core
A partial list of solutions I have for our issue:
- Make
@tanstack/query-corea peerDependency of all your packages even@tanstack/react-queryso the package.json of a user will look like this:
"@tanstack/query-core": "^4.23.0", "@tanstack/react-query": "^4.23.0", "@tanstack/react-query-persist-client": "^4.23.0",This would be similar to packages like @envelop or @dnd-kit
- You are not using ranges to allow your "plugins" to work with multiple cores so you could also set
@tanstack/query-coreas a dependency of all packages which need it. The package.json will end up like this:
"@tanstack/react-query": "^4.23.0", "@tanstack/react-query-persist-client": "^4.23.0",This what @sentry is doing with its packages
@sentry/reactwhere you can add@sentry/tracingI hope it makes sense to you now and keep mind I could be wrong on some parts as it's quite a tricky subject.
- Given a dependency
okay, let's try making it a
dependencythen. Would you like to contribute this?TkDodo closed this
Sign in to join this conversation.
I've seen this as well. The problem is that
persist-query-client-corehas a peerDependency onquery-core, but no one actually installsquery-core- because the framework adapters ship with the query-core built-in, e.g. react:https://github.com/TanStack/query/blob/f715b305dedca12ac22812145fa29e42dbd6dfa1/packages/react-query/src/index.ts#L6-L7
If we add it as a dependency instead of a peerDependency, wouldn't this mean that it would be shipped as a copy with the plugin?. Note that all framework adapters have a dependency on the query-core, so they bring the core with them. I would want to avoid the core being there twice 🤔 . Seems like I don't really understand what it means to have a
dependencyon something 😅 .But it works at runtime as it is right now, if you have one of the framework adapters, because they bring the core. So maybe what we'd want is an optional peerDependency? What I would like to express is: You need to have either a dependency on the query core, or on one of the framework adapters to make this plugin work. The persisters import directly from the core though:
https://github.com/TanStack/query/blob/715f0cc1fdb7acac332d829f203777c31a962947/packages/query-persist-client-core/src/persist.ts#L7
So it only works with a dependency on the react adapter because it has a dependency on the core itself ...