liveBook · Manning

Export

As explained in Secrets of the JavaScript Ninja, Second Edition

A JavaScript module keyword that makes bindings available for import by other modules.

An ES6 module’s exports are its public interface: other modules can access identifiers marked for export, while unexported top-level identifiers remain private to that module. Use export to share only what callers need; pair it with import to use those bindings elsewhere. section 11.2 section 11.2.1

Export identifiers in a few common ways:

  • Prefix a variable, function, or class declaration with export. section 11.2.1
  • Export declarations together at the end of a module, optionally giving an exported identifier a new name with as. section 11.2.1
  • Use export default for the module’s single default export; named and default exports can coexist. section 11.3

Named exports can be imported by name or renamed with as; a default export is imported without braces. Keep the exported interface intentional: unmarked bindings do not become available just because they sit at the top level. section 11.2.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains inline, grouped, default, and aliased exports, and demonstrates access boundaries through imports.
Examples
Uses a Ninja module to expose a greeting and function while keeping its ninja binding private, and exports a default Ninja class.
What you can do after
Apply named, grouped, default, and aliased exports to share selected variables, functions, or classes across modules.
How it connects
Connects export with import, ES6 modules, CommonJS, AMD, and the module pattern.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.