JS: Module. Static Import

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

Import Syntax for Default Export

import newName from moduleSpecifier;

import the default export value at moduleSpecifier and name it newName in your source code.

Import Syntax for Named Export

import { name1 , name2 , etc } from moduleSpecifier;

Import a list of names. Each of the name must be explicited exported by the module.

import { name1 as new1 , etc } from moduleSpecifier;

Import a list of exported names with new alias. Your code access the imported symbols by their new name, e.g. new1

import * as newName from moduleSpecifier;

Get all exported names from moduleSpecifier, attach to newName as properties.

Just Load Module (For Side-Effect)

This just load the module

import moduleSpecifier;

use this for modules that setup global variables or update browser methods, etc.

How Do You Know If a Module Has Named Export or Default Export

There is no general way. You just read the module's documentation or look at the source code.

JavaScript. Module Import Export