Nuxt.js: Proper way to create a store

Created on 22 May 2017  路  8Comments  路  Source: nuxt/nuxt.js

Me and my counterpart are working on the same project. And today we encountered a peculiar issue.

I'm sitting on MacOS with Node 7.10, and I have instantiated store this way:

const store = {
  state: { data: [] },
  mutations: {
    addData: state => payload => state.data.unshift(...payload)
  }
}

export default store

And everything works as intended. Even without importing and instantiating Vuex.

While my counterpart, on Ubuntu 16.10 with Node 7.7.6, gets an error with the same setting.

Nuxt.js Error:

Error: [nuxt] store/index.js should export a method which returns a Vuex instance.
    at getModule (.nuxt/store.js:53:10)
    at Object.module.exports.Object.defineProperty.value (.nuxt/store.js:16:14)

When in attempt to fix the issue I red an official example, and, instead of store object, I made a function which returns new store instance as example suggests:

import Vuex from 'vuex'

const store = () => {
  return new Vuex.Store({
    state: { data: [] },
    mutations: {
      addData: state => payload => state.data.unshift(...payload)
  })
}

export default store

The real fun begun here. The 'official' way does not work on my machine and causes store to be undefined.

I'm so much confused, I'd be very glad if anyone explain me what is going on.

Thank you in advance!

This question is available on Nuxt.js community (#c647)

Most helpful comment

I didn't noticed that in mine package.json, nuxt was locked on 0.10.7 and I didn't pushed it on remote, so my counterpart had [email protected], which, now I see, requires store to return an instance.

Case close, thank you everyone!

All 8 comments

@askhat You seem using classic store mode. You may modify your original code to something like this:

const store = {
  state: () => { data: [] },
  mutations: {
    addData: state => payload => state.data.unshift(...payload)
  }
}

export default store

@pi0 well, effectively putting function into state causes same error: it's undefined. I've tried a regular function () statement and an arrow function.

Now I'm not even sure which one is undefined store or state.

Have you tried this way ?

import Vuex from 'vuex'

const store = new Vuex.Store({
  state: {
    data: []
  },
  mutations: {
    ...
  }
})

export default store

Moreover, are the both of you on the same nuxt version ?

Hi @askhat

You need to export a createStore method, I will update the warning to be more explicit.

See the official example: https://github.com/nuxt/nuxt.js/blob/dev/examples/vuex-store/store/index.js

I didn't noticed that in mine package.json, nuxt was locked on 0.10.7 and I didn't pushed it on remote, so my counterpart had [email protected], which, now I see, requires store to return an instance.

Case close, thank you everyone!

My bad, no need to name it createStore. It works perfectly, see: https://glitch.com/edit/#!/nuxt-1-hello-world

Demo: https://nuxt-1-hello-world.glitch.me/

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gary149 picture gary149  路  3Comments

mikekidder picture mikekidder  路  3Comments

surmon-china picture surmon-china  路  3Comments

danieloprado picture danieloprado  路  3Comments

mattdharmon picture mattdharmon  路  3Comments