--- title: Fix "No Active Pinia" Error - Store Setup Timing impact: HIGH impactDescription: Using Pinia stores before app.use(pinia) causes "getActivePinia was called but there was no active Pinia" error type: gotcha tags: [vue3, pinia, state-management, setup, initialization, error] --- # Fix "No Active Pinia" Error - Store Setup Timing **Impact: HIGH** - The error "getActivePinia() was called but there was no active Pinia" is one of the most common Pinia errors. It occurs when you try to use a store before Pinia has been installed on the Vue app, causing your application to crash. ## Task Checklist - [ ] Ensure `app.use(pinia)` is called before `app.mount()` - [ ] Ensure `app.use(pinia)` is called before `app.use(router)` if router guards use stores - [ ] Never call `useXxxStore()` in module-level (top-level) code - [ ] Only call `useXxxStore()` inside setup functions, composables, or after app initialization - [ ] Check for ` ``` **Fix: Use ` ``` ```vue ``` ## Common Cause 4: mapStores with Parentheses ```vue ``` **Fix: Pass the function reference, not the result:** ```vue ``` ## Common Cause 5: Router Guards Before Pinia ```javascript // router/index.js - WRONG import { createRouter } from 'vue-router' import { useAuthStore } from '@/stores/auth' const router = createRouter({ /* ... */ }) // This guard is registered immediately router.beforeEach((to) => { // When this runs during app startup, Pinia might not be ready const authStore = useAuthStore() // May fail! if (to.meta.requiresAuth && !authStore.isLoggedIn) { return '/login' } }) ``` **Fix: Use lazy store access or ensure plugin order:** ```javascript // router/index.js - CORRECT import { createRouter } from 'vue-router' const router = createRouter({ /* ... */ }) router.beforeEach((to) => { // Dynamically import to avoid module-level execution const { useAuthStore } = await import('@/stores/auth') const authStore = useAuthStore() if (to.meta.requiresAuth && !authStore.isLoggedIn) { return '/login' } }) // OR ensure main.js has correct order: // app.use(pinia) // app.use(router) ``` ## Debugging Checklist When you see "No active Pinia": 1. **Check main.js order**: Is `app.use(pinia)` before other plugins? 2. **Search for top-level useStore calls**: Any store usage outside functions/setup? 3. **Check script tags**: Using `