1

Given the files below:

mutation-types.js

export const SET_VALUE = 'SET_VALUE'
export const SET_PLACEHOLDER = 'SET_PLACEHOLDER'

my-file.js

import * as types from './mutation-types'

gives an error saying Uncaught TypeError: types__WEBPACK_IMPORTED_MODULE_1_.default is undefined. Even the official vuex repo uses this syntax with the same version of Vue, 3. Here you can check it.

Why isn't it possible to use the import like import * as name from './something'?

kozhioyrin
  • 400
  • 4
  • 15

1 Answers1

1

You can see VueX is importing from ../api (api/index.js), which does not write export const as you do.

If you read the message closely it says ".default is undefined". So try it with export default.

Take an example from
Canonical way to define common constants in Vue.js

const SET_VALUE = 'SET_VALUE'
const SET_PLACEHOLDER = 'SET_PLACEHOLDER'

export default {
  SET_VALUE: SET_VALUE,
  SET_PLACEHOLDER: SET_PLACEHOLDER
}
Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
  • I don't think you understand me right. This duplicates the lines. In the example I gave it's not so. Even though I know it's kind a solution, I don't want to use `export default` and write the same lines again. I want to import all the defined variables/functions via `import * as xxx` like in that repo, so I can use `xxx.something` without having that `export default` duplication. – kozhioyrin Aug 13 '21 at 13:41
  • 1
    The repo you linked to does not export constant variables, it exports functions using const. The `../api` which is imported does use the `const` values but does not export them. The script which imports from `../api` does not use the constants, only the functions which were exported. It does not work the way you want. The error message definitely doesn't lie ".default is undefined".. so it expects an `export default` there. – Peter Krebs Aug 13 '21 at 14:16
  • 1
    I see now. So it's about the `const`. Thanks for clarifying it. – kozhioyrin Aug 13 '21 at 16:23