Uncaught ReferenceError: Cannot access 'store' before initialization

See original GitHub issue

I’m trying to get this working with an existing Vue 2 app but I’m getting the error:

Uncaught ReferenceError: Cannot access ‘store’ before initialization

I have a folder within:

src
  store
    index.js
    store.js
  main.js

main.js imports the store as such:

import store from './store/index.js';

and initialises the Vue instance like so:

export const app = new Vue({
  render: (h) => h(App),
  store,
  router,
  i18n,
}).$mount('#app');

index.js in the store folder looks like:

import Vue from 'vue';
import Vuex from 'vuex';
import { state, mutations, actions, getters } from './store.js';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
    }),
  ],
});

Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rcoundoncommented, Feb 23, 2021

It turns out this was due to a circular dependency. The store was importing the Vue app to reference i18n

0reactions
rcoundoncommented, Feb 23, 2021

@rcoundon Vite Server start or not ? The error from chrome console or terminal ? Give me the reproduction to help.

Yes, vite server started, it is an error in the browser console and the web application doesn’t render

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access 'store' before initialization in redux · Issue #167 ...
I'd suggest moving the configureStore() call into a separate store.js file instead, to break the dependency issue. 29
Read more >
ReferenceError: Cannot access 'Store' before initialization ...
Problem was solved by moving Store class to isolated file, before it was in the same file as the global store.
Read more >
ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
A lexical variable was accessed before it was initialized. This happens within any block statement, when variables declared with let or const ...
Read more >
cannot access 'store' before initialization - You.com | The AI ...
Problem was solved by moving Store class to isolated file, before it was in the same file as the global store. Open side...
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