question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error when building with vite-ssg

See original GitHub issue

Attempting to run npm build on my vitesse-based project and I’m seeing the following error:

...
[vite-ssg] Build for server...
vite v2.5.6 building SSR bundle for production...
✓ 78 modules transformed.
.vite-ssg-temp/manifest.webmanifest        0.38 KiB
.vite-ssg-temp/main.js                     137.98 KiB
.vite-ssg-temp/assets/style.1bff94d7.css   22.56 KiB
TypeError: Cannot read property '_s' of undefined
    at useStore (/home/weiner/Sites/hc_project/handcrafted/vue/node_modules/pinia/dist/pinia.cjs.js:1544:20)
    at Module.<anonymous> (/home/weiner/Sites/hc_project/handcrafted/vue/.vite-ssg-temp/main.js:2673:19)
    at Module.o._compile (/home/weiner/Sites/hc_project/handcrafted/vue/node_modules/jiti/dist/v8cache.js:2:2778)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at __require (/home/weiner/Sites/hc_project/handcrafted/vue/node_modules/vite-ssg/dist/chunk-3FHQZVYO.js:22:12)
    at build (/home/weiner/Sites/hc_project/handcrafted/vue/node_modules/vite-ssg/dist/node/cli.js:140:52)

Any idea if the issue is with pinia or with vite-ssg? The only ticket I can find that references both is https://github.com/antfu/vite-ssg/issues/72. Here’s the app init in my main.ts

// https://github.com/antfu/vite-ssg
// https://github.com/antfu/vite-ssg/issues/72
export const createApp = ViteSSG(
  App,
  {routes},
  (ctx) => {
    // install all modules under `modules/`
    Object.values(import.meta.globEager('./modules/*.ts')).map(i => i.install?.(ctx));
    const pinia = createPinia()
    pinia.use(({store}) => {
      store.router = markRaw(ctx.router)
    })

    ctx.app.use(pinia)
  },
)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mrweinercommented, Sep 11, 2021

It looks like the cause of this is using my store outside of a vue component.

/~/store/auth.ts

export const useAuthStore = defineStore({ ... })

/~/modules/cart.ts

import { useAuthStore } from "~/store/auth";
const authStore = useAuthStore();
...
export const cartApi = { ... }

Then cartApi is used around the app. Removing all of the store-related code from carts.ts allows the build to run fine, but useAuthStore() causes the build to break. Maybe I need to open a new issue over in vite-ssg to see how I should deal with this. I’d assume that the stores just don’t exist yet when the ssg is looking at the module or something.

1reaction
mrweinercommented, Oct 20, 2021

I just switched over to using vite-ssr instead of vite-ssg and encountered a different error from the same problem, so just figured I’d document my fix in here in case anybody stumbles across it. Initially I was defining my stores before the export, whereas now I do it in the export (which fixes my problem). This helps to ensure that pinia exists at that point, otherwise the stores may attempt to be defined before the application initializes and pinia is actually availabel:

Before

const authStore = useAuthStore();

export const cartApi = {
  // stuff
} 

After

export const useCartApi = () => {
  const authStore = useAuthStore();
  return {
    // stuff
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

error when building with vite-ssg #665 - vuejs/pinia - GitHub
I'm using pinia stores throughout the app so I'm initializing pinia here. The app runs as expected on localhost, but npm run build...
Read more >
Why i get Vitejs SSG Error when running npm run generate
I'm trying vite SSG and when runing the command npm run generate i get this error > generate > vite build --ssrManifest --outDir...
Read more >
Server-Side Rendering - Vite
When building an SSR app, you likely want to have full control over your main server and decouple Vite from the production environment....
Read more >
vite-ssg - npm
Server-side generation for Vite. Latest version: 0.22.1, last published: 13 days ago. Start using vite-ssg in your project by running `npm i ...
Read more >
Vite-ssg - npm.io
Check Vite-ssg 0.21.0 package - Last release 0.21.0 with MIT licence at our ... vite config file + "build": "vite-ssg build -c another-vite.config.ts"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found