ES6 Module

ES6 has built-in support for modules. Code can be divided into multiple modules and can be managed using export and import statements.

Requirements


To run the example given below NodeJS environment has to be set up with Babel.

Step 1. Define a project by creating package.json file.

{
  "name": "my-project",
  "version": "1.0.0",
  "devDependencies": {
    "babel-cli": "^6.0.0"
  },
  "dependencies": {
    "babel-preset-es2015": "^6.18.0"
  }
}

Step 2. Install packages using npm

> sudo npm install -g babel-cli
> npm install

Step 3. Create 3 Javascript files

  1. module1.js
  2. module2.js
  3. main.js

File 1 : module1.js




File 2 : module2.js


File 3 : main.js


Now run the script using node and babel by giving the command given below

> babel-node --presets=es2015 main

The output of this will be

5.5
3628800

Share