Re-exporting a Binding

There may be a time when you’d like to re-export something that your module has imported (for instance, if you’re creating a library out of several small modules). You can re-export an imported value with the patterns already discussed in this chapter as follows:

  1. import { sum } from "./example.js";
  2. export { sum }

That works, but a single statement can also do the same thing:

  1. export { sum } from "./example.js";

This form of export looks into the specified module for the declaration of sum and then exports it. Of course, you can also choose to export a different name for the same value:

  1. export { sum as add } from "./example.js";

Here, sum is imported from "./example.js" and then exported as add.

If you’d like to export everything from another module, you can use the * pattern:

  1. export * from "./example.js";

When you export everything, you are including all named exports and excluding any default export. For instance, if example.js has a default export, you would need to import it explicitly and then export it explicitly.