Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pinia: Cannot access store from Nuxt3 API

I have a Nuxt3 project with an Api in server/api/<api-endpoints>. Inside an endpoint, when I try to use a Pinia Store it says:

[nuxt] [request error] [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?

or when I try to access a composable function from composables it cannot define any variable (says xxx is undefined even though it works in components so it just can't define anything in the function).

Pinia example inside server/api/token

import { useAuthStore } from "../stores/useAuthStore";
export default async (req: any, res: any) => {
    const authStore = useAuthStore()
    return authStore.access_token
};

Composable example inside server/api/user

import { useUserLogin } from "../../composables/useUserLogin";
export default async (req: any, res: any) => {
    const userLogin = useUserLogin()
    return twitchLogin.is_logged_in
};
like image 621
MaxCodes Avatar asked Jun 19 '26 05:06

MaxCodes


1 Answers

I solved it by renaming buildModules to modules in nuxt.config.js

Nuxt modules are now build-time-only, and the buildModules property used in Nuxt 2 is deprecated in favor of modules.

// nuxt.config.js
export default defineNuxtConfig({
  modules: [
    // ...
    '@pinia/nuxt',
  ],
})
like image 177
un1t Avatar answered Jun 21 '26 11:06

un1t