pinia store in library mode doesn't work #2487
-
Reproductionhttps://github.com/aumaOne/vite-pinia-library Steps to reproduce the bugrun Expected behaviorbuild/preview would work the same as dev. Actual behaviorAll works fine when running with
Additional informationIt seems that the lib can't access the pinia instance when it is built. pinia.mjs
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I also have such a problem here, have you solved it? |
Beta Was this translation helpful? Give feedback.
-
I encountered the same issue and resolved it by passing the active Pinia store to the defined store as param, in your case in the reproduction, to the It seems that the problem may be related to how Vite resolves dependencies. Vite locates the nearest dependency to its root, and since Pinia is included in both the "my-app" and "my-lib" folders, it might be causing conflicts when importing a store from a other vite project / build. To troubleshoot this theory, consider removing Pinia from both projects and adding Pinia to the parent directory, placing it alongside "my-app" and "my-lib." as a shared dependency. Alternatively, consider adding an alias to both Vite configurations that targets the pinia dependency in the same node_modules folder. |
Beta Was this translation helpful? Give feedback.
-
You can use vite's shared options resolve.dedupe to resolve this issue import { defineConfig } from 'vite'
export default defineConfig(() => {
return {
// ...
resolve: {
dedupe: ['pinia'],
},
// ...
}
}) |
Beta Was this translation helpful? Give feedback.
-
Maybe this helps. https://pinia.vuejs.org/core-concepts/outside-component-usage.html |
Beta Was this translation helpful? Give feedback.
You can use vite's shared options resolve.dedupe to resolve this issue