Nuxt 3集成Pinia报错:__vite_ssr_import_0__.defineStore is not a function
Nuxt 3 配置 Pinia 异常问题解决
核心错误修复
你的 ~/stores/common.js 存在致命笔误:将 store 的唯一标识字段 id 误写为 if,这会导致 Pinia 无法正确初始化状态容器,直接触发 defineStore is not a function 错误,后续的内存溢出也是该错误引发的连锁反应。
修正后的 ~/stores/common.js 代码:
import { defineStore } from "pinia"; export const useStore = defineStore({ id: "common", // 此处将 if 改为 id state: () => { return { locale: "en", }; }, getters: {}, actions: {}, });
Nuxt 配置优化(推荐)
Nuxt 3 中 buildModules 已逐步被 modules 替代,建议调整配置如下,同时保留自动导入功能:
export default defineNuxtConfig({ target: "static", modules: [ [ "@pinia/nuxt", { autoImports: ["defineStore"], }, ], ], });
后续操作
完成上述修改后,执行以下命令清理缓存并重启服务:
rm -rf node_modules .nuxt package-lock.json npm install npm run dev
内存溢出问题是由 store 初始化错误引发的异常循环导致的,修复笔误后该问题会自动消失。若仍有异常,建议升级 Nuxt 和 Pinia 至稳定版本,避免 RC/Beta 版本的兼容性问题。
内容的提问来源于stack exchange,提问作者LeKevoid
相关产品推荐
相关产品推荐

