ES6 export all values from object

ModuleExportEcmascript 6

Module Problem Overview


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

So I can import them like:

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

The only way I found is by hard coding the exports:

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

Which is not dynamic.

Is it possible to export all values from an object?

Module Solutions


Solution 1 - Module

I can't really recommend this solution work-around but it does function. Rather than exporting an object, you use named exports each member. In another file, import the first module's named exports into an object and export that object as default. Also export all the named exports from the first module using export * from './file1';

values/value.js

let a = 1;
let b = 2;
let c = 3;

export {a, b, c};

values/index.js

import * as values from './value';

export default values;
export * from './value';

index.js

import values, {a} from './values';

console.log(values, a); // {a: 1, b: 2, c: 3} 1

Solution 2 - Module

Does not seem so. Quote from ECMAScript 6 modules: the final syntax:

> You may be wondering – why do we need named exports if we could simply default-export objects (like CommonJS)? The answer is that you can’t enforce a static structure via objects and lose all of the associated advantages (described in the next section).

Solution 3 - Module

I just had need to do this for a config 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

This is using Typescript mind you.

Solution 4 - Module

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);

Solution 5 - Module

Why not just do a named export of the object:

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

or

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

and then a named import where you need it:

import { values } from './my-module'

let foo = values.a
let { a, b, c } = values

or

import { values as myModule } from './my-module'

let foo = myModule.a
let { a, b, c } = myModule

can do default export as well:

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

or

export default { a: 1, b: 2, c: 3 }

and then consume it:

import whateverIcallIt from './my-Module'

let foo = whateverIcallIt.a
let {a, b, c } = whateverIcallIt

If you want to export a bunch of individual values, say a bunch of constants, you can:

export const a = 1
export const b = 2
//...

or even

export const a = 1,
             b = 2,
             c = 3,
//...

and then import them individually:

import { a, b, c } from './my-module'

Solution 6 - Module

I suggest the following, let's expect a module.js:

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 an 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

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

Solution 7 - Module

export const a = 1;
export const b = 2;
export const c = 3;

This will work w/ Babel transforms today and should take advantage of all the benefits of ES2016 modules whenever that feature actually lands in a browser.

You can also add export default {a, b, c}; which will allow you to import all the values as an object w/o the * as, i.e. import myModule from 'my-module';

Sources:

Solution 8 - Module

Solution 9 - Module

Every answer requires changing of the 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 can be useful e.g. if you want to validate the exported values with Joi or JSON Schema) then your my-module would 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 compiles to what you need.

See: Babel example

Solution 10 - Module

Exporting each variable from your variables file. Then importing them with * as in your other file and exporting the as a constant from that file will give you a dynamic object with the named exports from the first file being attributes on 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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmauvmView Question on Stackoverflow
Solution 1 - ModuleryanjduffyView Answer on Stackoverflow
Solution 2 - ModuleJoelView Answer on Stackoverflow
Solution 3 - ModulemikeyseeView Answer on Stackoverflow
Solution 4 - ModulespringuperView Answer on Stackoverflow
Solution 5 - ModulespeakingcodeView Answer on Stackoverflow
Solution 6 - ModuleRiZKiTView Answer on Stackoverflow
Solution 7 - ModuleJon zView Answer on Stackoverflow
Solution 8 - ModuleatilkanView Answer on Stackoverflow
Solution 9 - ModulerspView Answer on Stackoverflow
Solution 10 - ModuleTanya RandstoftView Answer on Stackoverflow