Reputation: 3646
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?
Upvotes: 165
Views: 169315
Reputation: 2501
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 destructuring 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 destructuring objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
Upvotes: 6
Reputation: 1506
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'
Upvotes: 7
Reputation: 5215
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
Upvotes: 107
Reputation: 23
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'
Upvotes: 2
Reputation: 5008
You can do lots of stupid thing with javascript. I will leave this quote here from YDKJS book.
Mentioned page of the book ->
Upvotes: 5
Reputation: 111268
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
Upvotes: 3
Reputation: 568
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);
Upvotes: 18
Reputation: 4562
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:
Upvotes: 4
Reputation: 1753
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.
Upvotes: 20
Reputation: 15742
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).
Upvotes: 51