The import
statement is used to import bindings which are exported by another module.
This feature is only just beginning to be implemented in browsers natively at this time. It is implemented in many transpilers, such as TypeScript and Babel, and bundlers such as Rollup, Webpack and Parcel.
import defaultExport from "module-name"; import * as name from "module-name"; import { export } from "module-name"; import { export as alias } from "module-name"; import { export1 , export2 } from "module-name"; import { export1 , export2 as alias2 , [...] } from "module-name"; import defaultExport, { export [ , [...] ] } from "module-name"; import defaultExport, * as name from "module-name"; import "module-name";
defaultExport
module-name
.js
file containing the module, excluding the .js
extension. Certain bundlers may permit or require the use of the extension; check your environment. Only single quotes and double quotes Strings are allowed.name
export, exportN
alias, aliasN
The name
parameter is the name of the "module object" which will be used as a kind of namespace to refer to the exports. The export
parameters specify individual named exports, while the import * as name
syntax imports all of them. Below are examples to clarify the syntax.
This inserts myModule
into the current scope, containing all the exports from the module in the file located in /modules/my-module.js
.
import * as myModule from '/modules/my-module.js';
Here, accessing the exports means using the module name ("myModule" in this case) as a namespace. For example, if the module imported above includes an export doAllTheAmazingThings()
, you would call it like this:
myModule.doAllTheAmazingThings();
Given an object or value named myExport
which has been exported from the module my-module
either implicitly (because the entire module is exported) or explicitly (using the export
statement), this inserts myExport
into the current scope.
import {myExport} from '/modules/my-module.js';
This inserts both foo
and bar
into the current scope.
import {foo, bar} from '/modules/my-module.js';
You can rename an export when importing it. For example, this inserts shortName
into the current scope.
import {reallyReallyLongModuleExportName as shortName} from '/modules/my-module.js';
Import multiple exports from a module with convenient aliases.
import { reallyReallyLongModuleExportName as shortName, anotherLongModuleName as short } from '/modules/my-module.js';
Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import any values.
import '/modules/my-module.js';
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 '/modules/my-module.js';
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 '/modules/my-module.js'; // myModule used as a namespace
or
import myDefault, {foo, bar} from '/modules/my-module.js'; // specific, named imports
Importing from a secondary module to assist in processing an AJAX JSON request.
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))); }
import { getUsefulContents } from '/modules/file.js'; 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 Latest Draft (ECMA-262) The definition of 'Imports' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 61 |
16 151 |
542 | No | 47 | 10.1 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | No | 61 | Yes | 542 | 47 | 10.1 | ? |
1. From version 15: this feature is behind the Experimental JavaScript Features
preference.
2. From version 54: this feature is behind the dom.moduleScripts.enabled
preference. To change preferences in Firefox, visit about:config.
export
© 2005–2018 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