Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1105435 of import

  • Revision slug: Web/JavaScript/Reference/Statements/import
  • Revision title: import
  • Revision id: 1105435
  • Created:
  • Creator: Wilfred
  • Is current revision? No
  • Comment link to relevant chromium bug for ES6 modules

Revision Content

{{jsSidebar("Statements")}}

The import statement is used to import functions, objects or primitives that have been exported from an external module, another script, etc.

Note: This feature is not implemented in any browsers natively at this time. It is implemented in many transpilers, such as the Traceur CompilerBabel or Rollup.

Syntax

import defaultMember from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as name from "module-name";
import "module-name";
name
Name of the object that will receive the imported values.
member, memberN
Name of the exported members to be imported.
defaultMember
Name of the exported default to be imported.
alias, aliasN
Name of the object that will receive the imported property
module-name
The name of the module to import. This is a file name.

Description

The name parameter is the name of the object that will receive the exported members. The member parameters specify individual members, while the name parameter imports all of them. name may also be a function if the module exports a single default parameter rather than a series of members. Below are examples to clarify the syntax.

Import an entire module's contents. This inserts myModule into the current scope, containing all the exported bindings from "my-module.js".

import * as myModule from "my-module";

Import a single member of a module. This inserts myMember into the current scope.

import {myMember} from "my-module";

Import multiple members of a module. This inserts both foo and bar into the current scope.

import {foo, bar} from "my-module";

Import a member with a more convenient alias. This inserts shortName into the current scope.

import {reallyReallyLongModuleMemberName as shortName} from "my-module";

Import multiple members of a module with convenient aliases.

import {reallyReallyLongModuleMemberName as shortName, anotherLongModuleName as short} from "my-module";

Import an entire module for side effects only, without importing any bindings.

import "my-module";

Importing defaults

It is possible to have default export (whether it is an object, a function, a class, etc.). Reciprocally, it is possible to use the import instruction to import such defaults.

The simplest version directly imports the default:

import myDefault from "my-module";

It is also possible to use the default syntax with the ones seen above (namespace imports or named imports). In such cases, the default import will have to be declared first. For instance:

import myDefault, * as myModule from "my-module";
// myModule used as a namespace

or

import myDefault, {foo, bar} from "my-module";
// specific, named imports

Examples

Importing a secondary file to assist in processing an AJAX JSON request.

// --file.js--
function getJSON(url, callback) {
  let xhr = new XMLHttpRequest();
  xhr.onload = function () { 
    callback(this.responseText) 
  };
  xhr.open("GET", url, true);
  xhr.send();
}

export function getUsefulContents(url, callback) {
  getJSON(url, data => callback(JSON.parse(data)));
}

// --main.js--
import { getUsefulContents } from "file";
getUsefulContents("https://www.example.com", data => {
  doSomethingUseful(data);
});

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-imports', 'Imports')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-imports', 'Imports')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support {{CompatNo}}[2] {{CompatNo}}[1] {{CompatNo}} Build 14342  {{CompatNo}} {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(36.0)}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatChrome(36.0)}}

[1] See {{bug("568953")}}.

[2] See Chromium bug 1569

See also

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong>import statement</strong> is used to import functions, objects or primitives that have been exported from an external module, another script, etc.</p>

<div class="note">
<p><strong>Note:</strong> This feature is not implemented in any browsers natively at this time. It is implemented in many transpilers, such as the <a href="https://github.com/google/traceur-compiler">Traceur Compiler</a>,&nbsp;<a href="https://babeljs.io/">Babel</a>&nbsp;or <a href="https://github.com/rollup/rollup">Rollup</a>.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
import <em>defaultMember</em> from "<em>module-name</em>";
import * as <em>name</em> from "<em>module-name</em>";
import { <em>member </em>} from "<em>module-name</em>";
import { <em>member</em> as <em>alias </em>} from "<em>module-name</em>";
import { <em>member1 , member2</em> } from "<em>module-name</em>";
import { <em>member1 , member2</em> as <em>alias2</em> , <em>[...]</em> } from "<em>module-name</em>";
import <em>defaultMember</em>, { <em>member</em> [ , <em>[...]</em> ] } from "<em>module-name</em>";
import <em>defaultMember</em>, * as <em>name</em> from "<em>module-name</em>";
import "<em>module-name</em>";</pre>

<dl>
 <dt><code>name</code></dt>
 <dd>Name of the object that will receive the imported values.</dd>
</dl>

<dl>
 <dt><code>member, memberN</code></dt>
 <dd>Name of the exported members to be imported.</dd>
 <dt><code>defaultMember</code></dt>
 <dd>Name of the exported default to be imported.</dd>
 <dt><code>alias, aliasN</code></dt>
 <dd>Name of the object that will receive the imported property</dd>
 <dt><code>module-name</code></dt>
 <dd>The name of the module to import. This is a file name.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>The&nbsp;<code>name</code>&nbsp;parameter is the name of the object that will receive the exported members. The&nbsp;<code>member</code> parameters specify individual members, while the&nbsp;<code>name</code> parameter imports all of them. <code>name</code> may also be a function if the module exports a single default parameter rather than a series of members. Below are examples to clarify the syntax.</p>

<p>Import an entire module's contents. This inserts&nbsp;<code>myModule</code>&nbsp;into the current scope, containing all the exported bindings from "my-module.js".</p>

<pre class="brush: js">
import * as <em>myModule</em> from "my-module";
</pre>

<p>Import a single member of a module. This inserts&nbsp;<code>myMember</code> into the current scope.</p>

<pre class="brush: js">
import {myMember} from "my-module";</pre>

<p>Import multiple members of a module. This inserts both&nbsp;<code>foo</code> and&nbsp;<code>bar</code> into the current scope.</p>

<pre class="brush: js">
import {foo, bar} from "my-module";</pre>

<p>Import a member with a more convenient alias. This inserts&nbsp;<code>shortName</code> into the current scope.</p>

<pre class="brush: js">
import {reallyReallyLongModuleMemberName as shortName} from "my-module";</pre>

<p>Import multiple members of a module with convenient aliases.</p>

<pre class="brush: js">
import {reallyReallyLongModuleMemberName as shortName, anotherLongModuleName as short} from "my-module";</pre>

<p>Import an entire module for side effects only, without importing any bindings.</p>

<pre class="brush: js">
import "my-module";</pre>

<h3 id="Importing_defaults">Importing defaults</h3>

<p>It is possible to have default export (whether it is an object, a function, a class, etc.). Reciprocally, it is possible to use the <code>import</code> instruction to import such defaults.</p>

<p>The simplest version directly imports the default:</p>

<pre class="brush: js">
import myDefault from "my-module";</pre>

<p>It is also possible to use the default syntax with the ones seen above (namespace imports or named imports). In such cases, the default import will have to be declared first. For instance:</p>

<pre class="brush: js">
import myDefault, * as myModule from "my-module";
// myModule used as a namespace</pre>

<p>or</p>

<pre class="brush: js">
import myDefault, {foo, bar} from "my-module";
// specific, named imports
</pre>

<h2 id="Examples">Examples</h2>

<p>Importing a secondary file to assist in processing an AJAX JSON request.</p>

<pre class="brush: js; highlight: [14]">
// --file.js--
function getJSON(url, callback) {
  let xhr = new XMLHttpRequest();
  xhr.onload = function () { 
&nbsp;   callback(this.responseText) 
&nbsp; };
  xhr.open("GET", url, true);
  xhr.send();
}

export function getUsefulContents(url, callback) {
  getJSON(url, data =&gt; callback(JSON.parse(data)));
}

// --main.js--
import { getUsefulContents } from "file";
getUsefulContents("https://www.example.com", data =&gt; {
  doSomethingUseful(data);
});</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-imports', 'Imports')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-imports', 'Imports')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Edge</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}<sup>[2]</sup></td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>Build 14342&nbsp;</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(36.0)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(36.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] See {{bug("568953")}}.</p>
<p>[2] See <a href="https://bugs.chromium.org/p/v8/issues/detail?id=1569">Chromium bug 1569</a></p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Statements/export", "export")}}</li>
 <li><a href="https://blogs.windows.com/msedgedev/2016/05/17/es6-modules-and-beyond/">Previewing ES6 Modules and more from ES2015, ES2016 and beyond</a></li>
 <li><a href="https://hacks.mozilla.org/2015/08/es6-in-depth-modules/">ES6 in Depth: Modules</a>, Hacks blog post by Jason Orendorff</li>
 <li><a class="external" href="https://exploringjs.com/es6/ch_modules.html">Axel Rauschmayer's book: "Exploring JS: Modules"</a></li>
</ul>
Revert to this revision