pinia icon indicating copy to clipboard operation
pinia copied to clipboard

Nuxt.js 2 + nuxt-vite + pinia error

Open maxdzin opened this issue 2 years ago • 6 comments

Reproduction

Thank you so much for such a great state managing lib for Vue. Pinia works well with Nuxt as well. However, when I tried to use it along with nuxt-vite, an issue happen from the start. I tried with the fresh install and also, there is a reproduction link to Codesandbox:

Steps to reproduce the behavior

  1. Initialize a fresh Nuxt universal app + install the dependencies (the latest versions are used at the moment): "dependencies": { "@nuxtjs/composition-api": "^0.29.2", "@pinia/nuxt": "0.0.2", "nuxt": "^2.15.8", "pinia": "^2.0.0-rc.9" }, "devDependencies": { "nuxt-vite": "^0.2.4" }
  2. Add required modules into "buildModules" section of nuxt.config,js: buildModules: ["nuxt-vite", "@nuxtjs/composition-api/module", "@pinia/nuxt"],
  3. Run npm run dev
  4. See the error

Expected behavior

The app launched correctly with listed modules/configurations.

Actual behavior

The app won't launch because of the error. This is comes in the console: Uncaught SyntaxError: The requested module '/node_modules/pinia/dist/pinia.cjs?import' does not provide an export named 'defineStore'

Additional information

node version is 14.17.3 npm version is 7.24.0

maxdzin avatar Sep 22 '21 18:09 maxdzin

I'm glad you like it ! I need a boiled down repro to check

posva avatar Sep 22 '21 20:09 posva

Hi @posva OK. I've created a repo for that. You can check it here: https://github.com/spirico/nvp-test.git

maxdzin avatar Sep 23 '21 06:09 maxdzin

It seems to be importing the wrong file of pinia but I'm not sure of why, could be a bug of nuxt-vite too.

posva avatar Sep 23 '21 16:09 posva

In the same setting (Nuxt 2 + vite) but with the most recent version of pinia, I got the following error Cannot read properties of undefined (reading 'install') in Function.Vue.use.

The relevant part of the generated server.js file looks as follows:

// --------------------
// Request: /@id/defaultexport:D:/Programming/JabRefOnline/node_modules/@pinia/nuxt/dist/templates/plugin.mjs
// Parents: 
// - /.nuxt/index.js ($id_d8486045)
// Dependencies: 
// - /node_modules/vue-demi/lib/index.mjs ($id_4c439bf5)
// - /node_modules/pinia/dist/pinia.mjs ($id_b1920624)
// --------------------
const $id_42c572ca = async function (global, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
const module = __createCJSModule__(__vite_ssr_exports__)
const __vite_ssr_import_0__ = await __vite_ssr_import__("/node_modules/vue-demi/lib/index.mjs");

const __vite_ssr_import_1__ = await __vite_ssr_import__("/@id/pinia/dist/pinia.mjs/dist/pinia.mjs");

if (__vite_ssr_import_0__.isVue2) {
  __vite_ssr_import_0__.Vue2.use(__vite_ssr_import_1__.PiniaVuePlugin);
}

The problem is the strange import statement for pinia. In fact, if I remove the following https://github.com/posva/pinia/blob/bd9a8a43b31163246d8a101aa27a94b86bfac4fd/packages/nuxt/src/index.ts#L31-L33 then the generated import reads

const __vite_ssr_import_1__ = await __vite_ssr_import__("/node_modules/pinia/dist/pinia.mjs");

and the error is gone.

tobiasdiez avatar Nov 01 '21 23:11 tobiasdiez

there's any update for this issue and pr #781?

antlionguard avatar May 11 '22 13:05 antlionguard

@posva https://stackblitz.com/github/Ttou/nuxt2-pinia-issue

Ttou avatar Jul 25 '22 01:07 Ttou