ES6 exports all values ​​from an object

Let's say I have a module ( ./my-module.js ) that has an object, which should be its return value:

 let values = { a: 1, b: 2, c: 3 } // "export values" results in SyntaxError: Unexpected token 

Therefore, I can import them, for example:

 import {a} from './my-module' // a === 1 import * as myModule from './my-module' // myModule.a === 1 

The only way I found is to hardcode the export:

 export let a = values.a export let b = values.b export let c = values.c // or: export let {a, b, c} = values 

What is not dynamic.

Is it possible to export all values ​​from an object?

+92
ecmascript-6 module export
Apr 24 '15 at 9:42
source share
9 answers

Does not look like it. Quote from ECMAScript 6 Modules: final syntax :

You might be wondering - why do we need to specify an export if we could just export default objects (like CommonJS)? The answer is that you cannot apply a static structure through objects and lose all the benefits associated with it (described in the next section).

+31
Apr 24 '15 at 19:44
source share

I cannot really recommend this solution, but it works. Instead of exporting an object, you use named export each member. In another file, import the first module named export into the object and export this object by default. Also export all named exports from the first module using export * from './file1';

<strong> values ​​/value.js

 let a = 1; let b = 2; let c = 3; export {a, b, c}; 

<strong> values ​​/ index.js

 import * as values from './values'; export default values; export * from './values'; 

index.js

 import values, {a} from './values'; console.log(values, a); // {a: 1, b: 2, c: 3} 1 
+66
Jun 30 '16 at 19:22
source share

try this ugly but workable solution:

 // use CommonJS to export all keys module.exports = { a: 1, b: 2, c: 3 }; // import by key import { a, b, c } from 'commonjs-style-module'; console.log(a, b, c); 
+12
Nov 20 '16 at 14:04
source share

I just needed to do this for the configuration file.

 var config = { x: "CHANGE_ME", y: "CHANGE_ME", z: "CHANGE_ME" } export default config; 

You can do it like this:

 import { default as config } from "./config"; console.log(config.x); // CHANGE_ME 

It uses a Typescript mind.

+7
May 05 '16 at
source share
 export const a = 1; export const b = 2; export const c = 3; 

This will work w / Babel converts today and should take full advantage of ES2016 modules whenever this function hits the browser.

You can also add export default {a, b, c}; , which allows you to import all values ​​as an object without * as , i.e. import myModule from 'my-module';

Sources:

+3
Jun 11 '16 at 17:43
source share

I suggest the following, let module.js be expected:

 const values = { a: 1, b: 2, c: 3 }; export { values }; // you could use default, but I'm specific here 

and then you can do in index.js :

 import { values } from "module"; // directly access the object console.log(values.a); // 1 // object destructuring const { a, b, c } = values; console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 // selective object destructering with renaming const { a:k, c:m } = values; console.log(k); // 1 console.log(m); // 3 // selective object destructering with renaming and default value const { a:x, b:y, d:z = 0 } = values; console.log(x); // 1 console.log(y); // 2 console.log(z); // 0 

Additional examples of destroying objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

+3
Apr 17 '18 at 9:48
source share

Each answer requires a change in import statements.

If you want to be able to use:

 import {a} from './my-module' // a === 1 import * as myModule from './my-module' // myModule.a === 1 

as in the question, and in your my-module you have everything that you need to export in one object (which may be useful, for example, if you want to check the exported values ​​using Joi or JSON Schema), then your my-module will have to be either:

 let values = { a: 1, b: 2, c: 3 } let {a, b, c} = values; export {a, b, c}; 

Or:

 let values = { a: 1, b: 2, c: 3 } export let {a, b, c} = values; 

Not pretty, but it matches what you need.

See Babel example

+2
Jul 26 '18 at 11:53
source share

Export each variable from your variables file. Then, importing them with *, as in your other file, and exporting as a constant from this file, you will get a dynamic object with a named export from the first file, which are attributes of the object exported from the second.

Variables.js

 export const var1 = 'first'; export const var2 = 'second': ... export const varN = 'nth'; 

Other.js

 import * as vars from './Variables'; export const Variables = vars; 

Third.js

 import { Variables } from './Other'; Variables.var2 === 'second' 
0
Jul 03 '19 at 9:27
source share



All Articles