import
Imports bindings exported by another ES module.
Syntax
import defaultExport from "module";
import { named1, named2 } from "module";
import * as ns from "module";
import "module";
Examples
// math.js exports: export function add(a, b) { return a + b; }
import { add } from "./math.js";
console.log(add(2, 3));
Output
5
// default export
import greet from "./greet.js";
console.log(greet("Ada"));
Output
Hello, Ada
// namespace import
import * as utils from "./utils.js";
console.log(typeof utils);
Output
object
Notes
- `import` declarations are hoisted and the bindings are live read-only
views of the exported values.
- Static imports must appear at the top level of a module. Use the
dynamic `import()` expression for conditional loading.
- Modules require `type="module"` in browsers or `.mjs` / `"type":
"module"` in Node.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 61 |
| firefox | 60 |
| safari | 10.1 |
| edge | 16 |
| node | 12.0 |