- modules are automatically strict mode
- declarations inside a module are scoped to that module (not visible by other scripts)
- ECMAScript does not define what
import
does (it’s left up to the implementation) - there is no error recovery; “if anything fails to load or link, nothing runs”
- exporting:
-
you can
export
any top-levelfunction
,class
,var
,let
, orconst
export function fn() { ... };
-
you can export lists, which can appear anywhere in a module’s top-level scope and can be combined with other
export
declarationsexport {a, b as bravo};
-
you can designate one of the exports as the default export
export default function fn() { ... }; // or in a list: export {a, b as bravo, fn as default};
-
you can re-export from other modules (similar to
import-from
followed byexport
, except that re-exported bindings won’t be added to the current module’s scope)export {c, d} from 'other'; export * from 'other'; // re-export everything
-
- importing:
-
you can import named exports (and optionally rename them)
import {a, bravo, c as charlie} from 'module';
-
you can import the default export of a module (incl. CommonJS or AMD modules) by ommiting the braces
// same value as require('lodash') in CommonJS import _ from 'lodash';
-
you can import both the default export and named exports at the same time
import _, {each, map} from 'lodash'; // or import {default as _, each, map} from 'lodash';
-
you can import a module’s namespace object, which contains all the module’s exports as properties
import * as user from 'user';
-
Read the full blog post here.
Related: https://dev.to/samanthaming/javascript-module-cheatsheet-5b4o