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'?

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
I see now. So it's about the const. Thanks for clarifying it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.