You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `import()` call, commonly called _dynamic import_, is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically into a potentially non-module environment.
17
+
18
+
Unlike the [declaration-style counterpart](/en-US/docs/Web/JavaScript/Reference/Statements/import), dynamic imports are only evaluated when needed, and permits greater syntactic flexibility.
19
+
20
+
## Syntax
21
+
22
+
```js
23
+
import(moduleName)
24
+
```
25
+
26
+
The `import()` call is a syntax that closely resembles a function call, but `import` itself is a keyword, not a function. You cannot alias it like `const myImport = import`, which will throw a {{jsxref("SyntaxError")}}.
27
+
28
+
### Parameters
29
+
30
+
-`moduleName`
31
+
- : The module to import from. The evaluation of the specifier is host-specified, but always follows the same algorithm as static [import declarations](/en-US/docs/Web/JavaScript/Reference/Statements/import).
32
+
33
+
### Return value
34
+
35
+
It returns a promise which fulfills to an object containing all exports from `moduleName`, with the same shape as a namespace import (`import * as name from moduleName`): an object with `null` prototype, and the default export available as a key named `default`.
36
+
37
+
## Description
38
+
39
+
The import declaration syntax (`import something from "somewhere"`) is static and will always result in the imported module being evaluated at load time. Dynamic imports allows one to circumvent the syntactic rigidity of import declarations and load a module conditionally or on demand. The following are some reasons why you might need to use dynamic import:
40
+
41
+
- When importing statically significantly slows the loading of your code and there is a low likelihood that you will need the code you are importing, or you will not need it until a later time.
42
+
- When importing statically significantly increases your program's memory usage and there is a low likelihood that you will need the code you are importing.
43
+
- When the module you are importing does not exist at load time.
44
+
- When the import specifier string needs to be constructed dynamically. (Static import only supports static specifiers.)
45
+
- When the module being imported has side effects, and you do not want those side effects unless some condition is true. (It is recommended not to have any side effects in a module, but you sometimes cannot control this in your module dependencies.)
46
+
- When you are in a non-module environment (for example, `eval` or a script file).
47
+
48
+
Use dynamic import only when necessary. The static form is preferable for loading initial dependencies, and can benefit more readily from static analysis tools and [tree shaking](/en-US/docs/Glossary/Tree_shaking).
49
+
50
+
If your file is not run as a module (if it's referenced in an HTML file, the script tag must have `type="module"`), you will not be able to use static import declarations, but the asynchronous dynamic import syntax will always be available, allowing you to import modules into non-module environments.
51
+
52
+
## Examples
53
+
54
+
### Import a module for its side effects only
55
+
56
+
```js
57
+
(async () => {
58
+
if (somethingIsTrue) {
59
+
// import module for side effects
60
+
awaitimport("/modules/my-module.js");
61
+
}
62
+
})();
63
+
```
64
+
65
+
If your project uses packages that export ESM, you can also import them for side
66
+
effects only. This will run the code in the package entry point file (and any files it
67
+
imports) only.
68
+
69
+
### Importing defaults
70
+
71
+
You need to destructure and rename the "default" key from the returned object.
72
+
73
+
```js
74
+
(async () => {
75
+
if (somethingIsTrue) {
76
+
const {
77
+
default:myDefault,
78
+
foo,
79
+
bar,
80
+
} =awaitimport("/modules/my-module.js");
81
+
}
82
+
})();
83
+
```
84
+
85
+
### Importing on-demand in response to user action
86
+
87
+
This example shows how to load functionality on to a page based on a user action, in this case a button click, and then call a function within that module. This is not the only way to implement this functionality. The `import()` function also supports `await`.
88
+
89
+
```js
90
+
constmain=document.querySelector("main");
91
+
for (constlinkofdocument.querySelectorAll("nav > a")) {
92
+
link.addEventListener("click", (e) => {
93
+
e.preventDefault();
94
+
95
+
import("/modules/my-module.js")
96
+
.then((module) => {
97
+
module.loadPageInto(main);
98
+
})
99
+
.catch((err) => {
100
+
main.textContent=err.message;
101
+
});
102
+
});
103
+
}
104
+
```
105
+
106
+
### Importing different modules based on environment
107
+
108
+
In processes such as server-side rendering, you may need to load different logic on server or in browser because they interact with different globals or modules (for example, browser code has access to web APIs like `document` and `navigator`, while server code has access to the server file system). You can do so through a conditional dynamic import.
109
+
110
+
```js
111
+
let myModule;
112
+
113
+
if (typeofwindow==="undefined") {
114
+
myModule =awaitimport("module-used-on-server");
115
+
} else {
116
+
myModule =awaitimport("module-used-in-browser");
117
+
}
118
+
```
119
+
120
+
### Importing modules with a non-literal specifier
121
+
122
+
Dynamic imports allow any expression as the module specifier, not necessarily string literals.
123
+
124
+
Here, we load 10 modules: `/modules/module-0.js`, `/modules/module-1.js`... in parallel, and call the `load` functions that each one exports.
export { variable1asname1, variable2asname2, …, nameN };
45
+
export { variable1as "stringname" };
45
46
46
47
// Exporting destructured assignments with renaming
47
48
exportconst { name1, name2:bar } = o;
@@ -62,8 +63,7 @@ export { default, … } from …;
62
63
```
63
64
64
65
-`nameN`
65
-
- : Identifier to be exported (so that it can be imported via
66
-
{{jsxref("Statements/import", "import")}} in another script).
66
+
- : 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.
used to import read-only live bindings which are [exported](/en-US/docs/Web/JavaScript/Reference/Statements/export) by
19
-
another module.
17
+
The static **`import`** declaration is used to import read-only live bindings which are [exported](/en-US/docs/Web/JavaScript/Reference/Statements/export) by another module.
20
18
21
19
Imported modules are in {{JSxRef("Strict_mode","strict mode")}}
22
20
whether you declare them as such or not. The `import` statement cannot be
23
21
used in embedded scripts unless such script has a `type="module"`. Bindings
24
22
imported are called live bindings because they are updated by the module that exported
25
23
the binding.
26
24
27
-
There is also a function-like dynamic **`import()`**, which
28
-
does not require scripts of `type="module"`.
25
+
There is also a function-like dynamic [`import()`](/en-US/docs/Web/JavaScript/Reference/Operators/import), which does not require scripts of `type="module"`.
29
26
30
27
Backward compatibility can be ensured using attribute `nomodule` on the
31
28
{{HTMLElement("script")}} tag.
@@ -37,28 +34,25 @@ import defaultExport from "module-name";
- : Name that will refer to the default export from the module.
47
+
- : Name that will refer to the default export from the module. Must be a valid JavaScript identifier.
50
48
-`module-name`
51
-
- : The module to import from. This is often a relative or absolute URL to the
52
-
`.js` file containing the module. Certain bundlers may permit or require
53
-
the use of the extension; check your environment. Only single quoted and double
54
-
quoted Strings are allowed.
49
+
- : The module to import from. The evaluation of the specifier is host-specified. This is often a relative or absolute URL to the `.js` file containing the module. In Node, extension-less imports often refer to packages in `node_modules`. Certain bundlers may permit importing files without extensions; check your environment. Only single quoted and double quoted Strings are allowed.
55
50
-`name`
56
-
- : Name of the module object that will be used as a kind of namespace when referring to
57
-
the imports.
51
+
- : Name of the module object that will be used as a kind of namespace when referring to the imports. Must be a valid JavaScript identifier.
58
52
-`exportN`
59
-
- : Name of the exports to be imported.
53
+
- : Name of the exports to be imported. The name can be either an identifier or a string literal, depending on what `module-name` declares to export. If it is a string literal, it must be aliased to a valid identifier.
60
54
-`aliasN`
61
-
- : Names that will refer to the named imports.
55
+
- : Names that will refer to the named imports. Must be a valid JavaScript identifier.
62
56
63
57
## Description
64
58
@@ -67,6 +61,10 @@ as a kind of namespace to refer to the exports. The `export` parameters
67
61
specify individual named exports, while the `import * as name` syntax imports
68
62
all of them. Below are examples to clarify the syntax.
69
63
64
+
`import` declarations are only permitted at the top-level of modules, and can only be present in module files. If an `import` declaration is encountered in non-module contexts (for example, script files, `eval`, `new Function`, which all have "script" or "function" as parsing goals), a `SyntaxError` is thrown. To load modules in non-module contexts, use the [dynamic import](/en-US/docs/Web/JavaScript/Reference/Operators/import) syntax instead.
65
+
66
+
`import` declarations are designed to be syntactically rigid (for example, only string literal specifiers, only permitted at top-level, all bindings must be identifiers...), which allows modules to be statically analyzed and synchronously linked before getting evaluated. This is the key to making modules asynchronous by nature, powering features like [top-level await](/en-US/docs/Web/JavaScript/Guide/Modules#top_level_await).
67
+
70
68
### Import an entire module's contents
71
69
72
70
This inserts `myModule` into the current scope, containing all the exports
@@ -84,6 +82,8 @@ namespace. For example, if the module imported above includes an export
84
82
myModule.doAllTheAmazingThings();
85
83
```
86
84
85
+
`myModule` is an object with `null` prototype, and the default export will be available as a key called `default`.
86
+
87
87
### Import a single export from a module
88
88
89
89
Given an object or value named `myExport` which has been exported from the
@@ -114,6 +114,18 @@ import {reallyReallyLongModuleExportName as shortName}
114
114
from '/modules/my-module.js';
115
115
```
116
116
117
+
A module may also export a member as a string literal which is not a valid identifier, in which case you must alias it in order to use it in the current module.
118
+
119
+
```js
120
+
// /modules/my-module.js
121
+
consta=1;
122
+
export { aas "a-b" };
123
+
```
124
+
125
+
```js
126
+
import { "a-b" asa } from"/modules/my-module.js";
127
+
```
128
+
117
129
### Rename multiple exports during import
118
130
119
131
Import multiple exports from a module with convenient aliases.
@@ -134,17 +146,6 @@ the module's global code, but doesn't actually import any values.
134
146
import'/modules/my-module.js';
135
147
```
136
148
137
-
This works with [dynamic imports](#dynamic_imports) as well:
138
-
139
-
```js
140
-
(async () => {
141
-
if (somethingIsTrue) {
142
-
// import module for side effects
143
-
awaitimport('/modules/my-module.js');
144
-
}
145
-
})();
146
-
```
147
-
148
149
If your project uses packages that export ESM, you can also import them for side
149
150
effects only. This will run the code in the package entry point file (and any files it
150
151
imports) only.
@@ -177,54 +178,10 @@ import myDefault, {foo, bar} from '/modules/my-module.js';
177
178
// specific, named imports
178
179
```
179
180
180
-
When importing a default export with [dynamic imports](#dynamic_imports), it
181
-
works a bit differently. You need to destructure and rename the "default" key from the
182
-
returned object.
183
-
184
-
```js
185
-
(async () => {
186
-
if (somethingIsTrue) {
187
-
const { default:myDefault, foo, bar } =awaitimport('/modules/my-module.js');
188
-
}
189
-
})();
190
-
```
191
-
192
-
### Dynamic Imports
193
-
194
-
The standard import syntax is static and will always result in all code in the imported
195
-
module being evaluated at load time. In situations where you wish to load a module
196
-
conditionally or on demand, you can use a dynamic import instead. The following are some
197
-
reasons why you might need to use dynamic import:
198
-
199
-
- When importing statically significantly slows the loading of your code and there is
200
-
a low likelihood that you will need the code you are importing, or you will not need
201
-
it until a later time.
202
-
- When importing statically significantly increases your program's memory usage and
203
-
there is a low likelihood that you will need the code you are importing.
204
-
- When the module you are importing does not exist at load time
205
-
- When the import specifier string needs to be constructed dynamically. (Static import
206
-
only supports static specifiers.)
207
-
- When the module being imported has side effects, and you do not want those side
208
-
effects unless some condition is true. (It is recommended not to have any side effects
209
-
in a module, but you sometimes cannot control this in your module dependencies.)
210
-
211
-
Use dynamic import only when necessary. The static form is preferable for loading
212
-
initial dependencies, and can benefit more readily from static analysis tools and [tree shaking](/en-US/docs/Glossary/Tree_shaking).
213
-
214
-
To dynamically import a module, the `import` keyword may be called as a
215
-
function. When used this way, it returns a promise.
216
-
217
-
```js
218
-
import('/modules/my-module.js')
219
-
.then((module) => {
220
-
// Do something with the module.
221
-
});
222
-
```
223
-
224
-
This form also supports the `await` keyword.
181
+
Importing a name called `default` has the same effect as a default import. It is necessary to alias the name because `default` is a reserved word.
0 commit comments