Sunday, May 19, 2024
155
rated 0 times [  156] [ 1]  / answers: 1 / hits: 15793  / 6 Years ago, tue, august 28, 2018, 12:00:00

Given the following modules, how do I import the constants module and avoid having the default property included:



// constants.es6
export default {
foo: 'foo',
bar: 'bar'
}

// anotherModule.es6
import * as constants from './constants';


results in constants.default.foo



I can't seem to get the syntax correct to end up with constants.foo


More From » ecmascript-6

 Answers
12

You shouldn't use an object for defining constants. Calling code is free to do constants.foo = 42; and change the value.



Use



export const foo = 'foo';
export const bar = 'bar';


instead.



Then the import statement you have, import * as constants from './constants';, will work as well.






If you don't want to change the way you define the constants, then your question is rather how do I import a default export, which is answered in these questions:




[#53632] Friday, August 24, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;