N
Nuxt4w ago
dmarr

Trouble with pinia - `defineStore is not defined` when a layer containing a pinia store is called

I have been fighting this issue for awhile now, but when I have a layer in my app that is packaged separately (non-monorepo) and it uses pinia, I get errors in my app: defineStore is not defined -- I have tried so many things to work around this, and even have a PR that I think fixes the issue on pinia: https://github.com/vuejs/pinia/pull/2699 Maybe this is a shot in the dark, but if anyone can help me understand where/why that is failing it would be a big help. If I look in the imports.d.ts file in my app's .nuxt dir, I can see that defineStore is exported:
export { defineStore, acceptHMRUpdate, usePinia, storeToRefs } from '../node_modules/@pinia/nuxt/dist/runtime/composables';
export { defineStore, acceptHMRUpdate, usePinia, storeToRefs } from '../node_modules/@pinia/nuxt/dist/runtime/composables';
I am not sure how to troubleshoot this further.
GitHub
feat(nuxt): Support nuxt layers in a better way by marr · Pull Requ...
When using the pinia nuxt module with nuxt layers, there were some things that don't appear to work correctly. I am trying to solve for a nuxt layer workflow, which adds the pinia module an...
3 Replies
kapa.ai
kapa.ai4w ago
Hi @dmarr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, I don't have enough specific details to directly address your issue with defineStore is not defined in a layer containing a Pinia store. However, I can provide some insights and suggestions based on related information: 1. Pinia Integration: Nuxt 3 has built-in support for Pinia through the @pinia/nuxt module. Make sure you have properly installed and configured this module in your Nuxt project. 2. Module Configuration: Ensure that you have added '@pinia/nuxt' to the modules array in your nuxt.config.ts file:
export default defineNuxtConfig({
modules: ['@pinia/nuxt']
})

export default defineNuxtConfig({
modules: ['@pinia/nuxt']
})

3. Importing defineStore: In your Pinia store files, make sure you're importing defineStore correctly:
import { defineStore } from 'pinia'

import { defineStore } from 'pinia'

4. Layer Dependencies: There might be an issue with how dependencies are managed in layers. According to a GitHub issue comment, layers should be able to use their own dependencies, but they don't expose them to the final project. If you're using a git source for your layer, you might need to consider publishing it as an npm package or using a monorepo approach for local development. 5. Pinia Version Compatibility: Ensure that you're using compatible versions of Pinia and @pinia/nuxt. There have been recent discussions about major version updates for Pinia, which might affect compatibility. YouTube: Nuxt 3: MAJOR versions for Pinia and VueUse 6. Hydration Issues: While not directly related to your specific error, be aware that using Pinia stores can sometimes lead to hydration mismatches, especially when dealing with server-side rendering. Stack Overflow: Hydration completed but contains mismatches using VeeValidate and Pinia in Nuxt Given the complexity of your issue and the lack of specific information in the provided knowledge sources, I recommend: 1. Double-checking your Pinia and @pinia/nuxt versions and ensuring they're compatible with your Nuxt version. 2. Verifying that Pinia is properly configured in your Nuxt config file. 3. Considering the use of a monorepo for local development of your layer, as suggested in one of the GitHub issues. 4. If the issue persists, you might want to create a minimal reproduction of the problem and seek help on the Nuxt or Pinia GitHub issues, as your case seems quite specific and might require more in-depth investigation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@139883350096740354> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
dmarr
dmarrOP4w ago
as additional context, if I npm link the layer dep, the error does not happen. and in that case the imports.d.ts file points to the location of the layer:
export { defineStore, acceptHMRUpdate, usePinia, storeToRefs } from '../../my-layer/node_modules/@pinia/nuxt/dist/runtime/composables';
export { defineStore, acceptHMRUpdate, usePinia, storeToRefs } from '../../my-layer/node_modules/@pinia/nuxt/dist/runtime/composables';
Finally figured it out. My layer had a srcDir set. The storesDirs weren’t set relative to the layer using createResolver.

Did you find this page helpful?