Search code examples
javascriptnode.jsecmascript-6es6-modulescommonjs

List all exports in ES6 Module syntax


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
// :(

Solution

  • ES 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.