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:
import { sum } from "./example.js";
export { sum }
That works, but a single statement can also do the same thing:
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:
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:
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.