最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - List all exports in ES6 Module syntax - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论