Path: blob/main/files/en-us/web/javascript/reference/statements/export/index.md
6520 views
------{{jsSidebar("Statements")}}
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the {{jsxref("Statements/import", "import")}} declaration or dynamic import. The value of an imported binding is subject to change in the module that exports it — when a module updates the value of a binding that it exports, the update will be visible in its imported value.
In order to use the export declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type="module" to the {{HTMLElement("script")}} tag, or by being imported by another module. Modules are automatically interpreted in strict mode.
Syntax
nameN: Identifier to be exported (so that it can be imported via {{jsxref("Statements/import", "import")}} in another script). If you use an alias with
as, the actual exported name can be specified as a string literal, which may not be a valid identifier.
Description
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export. Each type corresponds to one of the above syntax.
Named exports:
After the export keyword, you can use let, const, and var declarations, as well as function or class declarations. You can also use the export { name1, name2 } syntax to export a list of names declared elsewhere. Note that export {} does not export an empty object — it's a no-op declaration that exports nothing (an empty name list).
Export declarations are not subject to temporal dead zone rules. You can declare that the module exports X before the name X itself is declared.
Default exports:
Note: Names for export declarations must be distinct from each other. Having exports with duplicate names or using more than one
defaultexport will result in a {{jsxref("SyntaxError")}} and prevent the module from being evaluated.
The export default syntax allows any expression.
As a special case, functions and classes are exported as declarations, not expressions, and these declarations can be anonymous. This means functions will be hoisted.
Named exports are useful when you need to export several values. When importing this module, named exports must be referred to by the exact same name (optionally renaming it with as), but the default export can be imported with any name. For example:
You can also rename named exports to avoid naming conflicts:
You can rename a name to something that's not a valid identifier by using a string literal. For example:
Re-exporting / Aggregating
A module can also "relay" values exported from other modules without the hassle of writing two separate import/export statements. This is often useful when creating a single module concentrating various exports from various modules (usually called a "barrel module").
This can be achieved with the "export from" syntax:
Which is comparable to a combination of import and export, except that function1 and function2 do not become available inside the current module:
Most of the "import from" syntaxes have "export from" counterparts.
There is also export * from "mod", although there's no import * from "mod". This re-exports all named exports from mod as the named exports of the current module, but the default export of mod is not re-exported. If there are two wildcard exports statements that implicitly re-export the same name, neither one is re-exported.
Attempting to import the duplicate name directly will throw an error.
The following is syntactically invalid despite its import equivalent:
The correct way of doing this is to rename the export:
The "export from" syntax allows the as token to be omitted, which makes the default export still re-exported as default export.
Examples
Using named exports
In a module my-module.js, we could include the following code:
Then in the top-level module included in your HTML page, we could have:
It is important to note the following:
You need to include this script in your HTML with a {{htmlelement("script")}} element of
type="module", so that it gets recognized as a module and dealt with appropriately.You can't run JS modules via a
file://URL — you'll get CORS errors. You need to run it via an HTTP server.
Using the default export
If we want to export a single value or to have a fallback value for your module, you could use a default export:
Then, in another script, it is straightforward to import the default export:
Using export from
Let's take an example where we have the following hierarchy:
childModule1.js: exportingmyFunctionandmyVariablechildModule2.js: exportingMyClassparentModule.js: acting as an aggregator (and doing nothing else)top level module: consuming the exports of
parentModule.js
This is what it would look like using code snippets:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Statements/import", "import")}}
JavaScript modules guide
ES6 in Depth: Modules, Hacks blog post by Jason Orendorff
ES modules: A cartoon deep-dive, Hacks blog post by Lin Clark