Nuxt 3 with Pinia gives me error 500
Whenever I import my store to a page/layout/component, I'm getting a 500 error (
[Vue Router warn]: uncaught error during route navigation: error: nuxt instance unavailable
) on initial load.
On my error page, I have a home button (const handleClearError = () => clearError({ redirect: '/' })
which brings me back to the main page. Strangely, I can press this button and everything (including the Pinia store) works perfectly. The problem is just the initial page load (problem occurs every time I refresh the browser page).
In short: On whatever layout/page/component I import import { useUserStore } from '~~/stores/user';
, this problem will occur on page load.
While installing Pinia, I just followed the documentation (https://pinia.vuejs.org/ssr/nuxt.html). I read somewhere else that there is a problem with npm and Pinia right now so I tried to install the pinia/nuxt dev + normal dependency with yarn and npm. Same problem every time.
Is this a known issue with Nuxt 3 + Pinia or am I missing something here?28 Replies
Is the @pinia/nuxt package registered in the modules array in the conig? And could you show us the store file?
I am using nuxt3 and pinia on multiple projects, and with ssr, and it works fine.
Yes, it's registered
The autoImports: [] was added later on. The problems occurs both when I manually define the store or auto import it.
That’s interesting… let me compare that to my working repo, maybe I’ll find something. But by the looks everything looks well implemented.
Thank you very much!
Could you maybe also show an example on how this store is used in a component/page?
I've tried using it within my script setup and in the template.
Even when I don't use the store, the error occurs. This line
import { useUserStore } from '~~/stores/user';
is enough to break it somehow.
These are the 2 instances in my login.vue
page
[Vue warn]: Unhandled error during execution of setup function at <Anonymous key="default" name="default" hasTransition=false >
This is another error that i get in my terminal whenever i import the useUserStore
I suppose you’re doing const store = useUserStore() in the root level of the <script setup> right?
Does the <template> have multiple tags directly inside? Or is there a <div> for example that wraps them all?
Yes, the first lines are :
this is the beginning of the template. after that, the login form follow
Could you try importing it in a different way? So maybe as an absolute path, like you do for useAuthUser
So there is only one div directly under template, right?
Otherwise, could you try creating a new blank page Vue component and doing nothing but importing and using the store? Since i don’t see any issues from your code, i think it could have to do with something installed in your codebase/on your page. Since when I would now create a fresh new nuxt project and add Pinia to it, it all works.
Those are my dependencies for my working project:
i'll try a blank page
i see you have both @pinia/nuxt and pinia installed as a dependency
i got @pinia/nuxt as dev dependency
Yeah devDependencies and dependency are almost the same on modern frontends, but sometimes it can make a difference.
This page alone breaks it
nothing else in it
I'll take your package.json and play around with the versions for a bit
Did you install your pinia deps with yarn or npm?
I installed all with npm. Yeah maybe try to install the exact same versions I have of both packes
Because that’s very interesting, if the fresh new page also breaks it
deleted node_modules/ package-lock.json, installed all pinia/nuxt and pinia with exactly your versions and still got the error O_O.
Would you mind sending me your package.json so I can install exactly your app and see if it still breaks?
Maybe also delete the .nuxt folder completely
That’s strange 😄
That's my full package.json
Thats my store definition
This is an example vue component where I use it, which works
Here's the full repo https://github.com/madebyfabian/zingio
Yep, that toally works
Probably a dependency issue with some other library?
Can't really think of anything else
Nice, so it works after deleting the .nuxt folder?
Yeah I also think it’s that. some bug which caused nuxt not to generate its files properly
The fresh install with your package.json works. deleting .nuxt and node_modules unfortunately didn't fix my app
I guess I'll just have to install each lib one by one and see when it breaks to find the culprit
Alright, glad it works now.
Would be interested which package causes this, let me know if you find out
Found it!
const supabase = useSupabaseClient()
using the supabase client in the store or importing it from another js module 500s the app
Do you think that's some kind of issue with SSR?
I'm using exactly the same setup in a Quasar app (client side rendingering) where I use Supabase queries in my Pinia store and everything works fine.Ah interesting. Yeah I think that's an SSR error. Since the
useSupabaseClient
is client-side only. On server-side there is a different method and you have to pass the server event into the method.
What you could do here is ensure that your page is a SPA, or that your component is wrapped in <ClientOnly>
. Do you need it to be SSR?
Or, what may fit better if you really need SSR, is to move that supabase call completely onto a /server/api/... route, and then call this route with $fetch in the client
Like in this example: https://supabase.nuxtjs.org/usage/services/server-supabase-client
and on the client
Yeah that makes total sense. I moved the whole supabase query to my Login.vue
<script setup>
and send the fetched data to my Pinia store and everything works fine. That's a solution that works for me, I don't really insist on having that whole function in my Store 😄
I like the /server/api/route... way for my more sensitive supabase api calls. I'll read up on the server-side supabase functions, that sounds really interesting. So far, I've only been using supabase without SSR because the RLS is so convenient.
thanks a lot for your help, fabian! really appreciated@erztemplerobba Sure, glad I could helped. Yeah that's something I also had to find out a bit and maybe there are even better ways. But since nuxt3 and the nuxt3/supabase module is so relatively "brand-new", I think it just needs time to evolve some kind of best practices and standards.