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 Compiler, Babel, Rollup or Webpack.
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
member, memberN
defaultMember
alias, aliasN
module-name
The name
parameter is the name which will refer to the exported members. The member
parameters specify individual members, while the name
parameter imports all of them. 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 the module identified by "my-module", often "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 an entire module for side effects only, without importing any bindings.
import 'my-module';
Import multiple members of a module with convenient aliases.
import {reallyReallyLongModuleMemberName as shortName, anotherLongModuleName as short} from 'my-module';
It is possible to have a default export (whether it is an object, a function, a class, etc.). The import
statement may then be used 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
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('http://www.example.com', data => { doSomethingUseful(data); });
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Imports' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Imports' in that specification. | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No support[2] | Build 14342[3] | No support[1] | No support | No support | Tech Preview 21[4] |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | No support | 36.0 | No support | No support | No support | No support | 36.0 |
[1] See bug 568953.
[2] See Chromium bug 1569
[3] Behind the "Enable experimental JavaScript features" flag
[4] See Safari Technical Preview 21 Release Notes
export
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import