In CommonJS, one can get all exported properties like so:
module.exports.foo = 1234;
module.exports.bar = 5678;
console.log(module.exports); // <-- The variable `exports`/`module.exports` holds an object
// :)
How can I do the equivalent with ES6 module syntax?
export const foo = 1234;
export const bar = 5678;
console.log(module.exports); // <-- Correctly holds the keys/export names, but not their values
// :(
In CommonJS, one can get all exported properties like so:
module.exports.foo = 1234;
module.exports.bar = 5678;
console.log(module.exports); // <-- The variable `exports`/`module.exports` holds an object
// :)
How can I do the equivalent with ES6 module syntax?
export const foo = 1234;
export const bar = 5678;
console.log(module.exports); // <-- Correctly holds the keys/export names, but not their values
// :(
Share
Improve this question
edited Mar 15, 2021 at 20:31
3x071c
asked Mar 15, 2021 at 19:41
3x071c3x071c
1,01615 silver badges44 bronze badges
3
- What are you actually trying to do with this? – loganfsmyth Commented Mar 15, 2021 at 19:53
-
@loganfsmyth Umm, writing more concise code? I need an object containing all functions I export, and ideally, I don't want to repeat myself/write out the function names manually again, and instead grab the
exports
object (if such a thing even exists in ES6). I kept the question short for the sake of simplicity. – 3x071c Commented Mar 15, 2021 at 20:33 - Fair enough, just trying to make sure whatever I write will actually address what you need. – loganfsmyth Commented Mar 15, 2021 at 20:48
1 Answer
Reset to default 7ES modules have
import * as ModuleObj from "./foo";
to import a namespace object containing all of the exports of a module.
For the usecase of module.exports
, you can have the module import itself.