1# ES6 module support for JerryScript 2 3The module system allows users to write import and export statements in scripts, which can be used to separate the logic of the application into custom modules. 4The standard's relevant part can be found [here](https://www.ecma-international.org/ecma-262/6.0/#sec-modules). 5Embedders wishing to use native builtin modules with ES6 imports can use the [Port API](05.PORT-API.md#es2015-module-system) to do so. 6 7## General 8 9If a script contains import statements, then JerryScript will open and evaluate the the referenced modules before the main script runs, resolving and creating bindings for the referenced identifiers in the process. 10It is not necessary to use any specific filename extensions for modules, JerryScript will try to open the given file paths as they are, but will try to normalize them before doing so. The exact normalization process is dependant on the port implementation provided. It is the user's responsibility to verify that the given files are valid EcmaScript modules. 11 12main.js 13 14```js 15import { exported_value } from "./module.js" 16 17print (exported_value); 18``` 19 20module.js 21 22```js 23var exported_value = 42; 24 25export exported_value; 26``` 27 28## Supported features 29 30* exporting identifiers from the module's lexical environment 31 * specifying export names for the exported values 32* importing exported identifiers from a module 33 * specifying local binding names for the imported values 34* module namespace imports 35 * `import * as module from 'module.js` 36* indirect export statements 37 * `export {variable} from 'module.js'` 38* star export statements 39 * `export * from 'module.js'` 40* importing a module for side-effects 41 * `import 'module.js'` 42* default import and export statements 43 * `export default local_identifier` 44 * `import def from 'module.js'` 45* anonymous default exports 46 * `export default function () {}` 47 48### Example 49 50```js 51import { 52 engine, 53 version as v 54} from "./module.js" 55 56import { getFeatureDetails } from "./module_2.js" 57 58var version = "v3.1415"; 59 60print("> main.js"); 61 62print(">> Engine: " + engine); 63print(">> Version: " + v); 64 65print (">> " + getFeatureDetails()); 66print (">> Script version: " + version); 67``` 68 69```js 70// module.js 71var _engine = "JerryScript"; 72export _engine as engine; 73 74export var version = "1.0 (e92ae0fb)"; 75``` 76 77```js 78// module_2.js 79var featureName = "EcmaScript 2015 modules"; 80var year = 2018; 81 82export function getFeatureDetails() { 83 return "Feature name: " + featureName + " | developed in " + year; 84} 85``` 86 87### Module namespace import statements 88 89A module namespace object can be imported. In this case the local binding will contain an object holding the exported values of the module, including local exports and all indirect exports. Ambiguous exported names are exluded from the namespace object. 90 91```js 92import * as module from './module.js'; 93 94print(">> Engine: " + module.engine); 95print(">> Version: " + module.version); 96``` 97 98### Indirect export statements 99 100An export statement can transitively export variables from another module, either via named indirect exports or a star export statement. In this case the resolving process will follow the chain until it reaches a module containing a local binding for that export name. If there are multiple modules which satisfy the export, that means the export is ambiguous, and will result in a SyntaxError. 101 102```js 103import { a, b } from 'module.js' 104 105print (a + b); 106``` 107 108```js 109// module.js 110export var a = 2; 111export { b } from 'module2.js' 112``` 113 114```js 115// module2.js 116export var b = 40; 117``` 118 119### Default imports and exports 120 121Each module can optionally provide a single default export by using the `export default` statement. Default exports can either reference identifiers in the module's lexical environment, or be an anonymous default export, in which case they will only be accessible by an importing script. 122 123```js 124import defaultExport, { b as c } from 'module.js' 125 126print (defaultExport); // 2 127print (c ()); // 42 128``` 129 130```js 131// module.js 132export default 2; 133export function b () { 134 return 42; 135} 136``` 137 138### Importing modules for side-effects 139 140Evaluate a module without importing anything. Any errors encountered in the module will be propagated. 141 142```js 143import 'module.js' // > module.js 144// "> module.js" is printed 145b (); // (ReferenceError) b is not defined 146``` 147 148```js 149// module.js 150export function b () { 151 print ("> module.js"); 152 return 42; 153} 154b (); 155``` 156 157## Unsupported features 158 159* **snapshot** 160