Nuxt 3 snippets
I recently work a lot with Nuxt 3 and I noticed that we don't have much information about it. We can find some basics on nuxt.com but the more you want to implement the more you need to search elsewhere.
Pinia and "Cannot find module 'pinia/dist/pinia.mjs'" error
Sometimes you might encounter this weird error Cannot find module 'pinia/dist/pinia.mjs'
. You have your Nuxt 3 with Pinia module everything just works and one day during npm run dev
you see this error. The quickest fix is to add these 3 lines to your nuxt.config.js
:
javascript
alias: {
pinia: "/node_modules/@pinia/nuxt/node_modules/pinia/dist/pinia.mjs"
},
Source: StackOverflow
Get current host on server and client side
Window object isn't available on server side, so we need to grab it from SSR context. Something like this:
javascript
const hostname = process.server ?
useNuxtApp().ssrContext.event.node.req.headers.host :
window.location.hostname
COMMENTS