# Using ES6 Modules Today

DevFeed: [Using ES6 Modules Today](<https://devfeed.tech/articles/using-es6-modules-today-52555.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/s6-modules-today/>)

Published: 2014-06-08T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [ECMAScript](<https://devfeed.tech/topics/ecmascript.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [modules](<https://devfeed.tech/topics/modules.md>), [Code](<https://devfeed.tech/topics/code.md>), [CommonJS](<https://devfeed.tech/topics/commonjs.md>), [export](<https://devfeed.tech/topics/export.md>), [import](<https://devfeed.tech/topics/import.md>), [syntax](<https://devfeed.tech/topics/syntax.md>), [function](<https://devfeed.tech/topics/function.md>), [App](<https://devfeed.tech/topics/app.md>), [make](<https://devfeed.tech/topics/make.md>)

Tags: [commonjs](<https://devfeed.tech/tags/commonjs.md>), [es6](<https://devfeed.tech/tags/es6.md>), [export](<https://devfeed.tech/tags/export.md>), [function](<https://devfeed.tech/tags/function.md>), [gulp](<https://devfeed.tech/tags/gulp.md>), [import](<https://devfeed.tech/tags/import.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [modules](<https://devfeed.tech/tags/modules.md>), [npm](<https://devfeed.tech/tags/npm.md>), [syntax](<https://devfeed.tech/tags/syntax.md>)

## AI overview

A tutorial on using ES6 module syntax with Square's ES6 Module Transpiler. It demonstrates exporting and importing JavaScript modules, compiling them to CommonJS or AMD, and running the compiled code with Node or RequireJS.

## Source excerpt

Continuing with the theme of playing with new features of ES6, today we're going to look at how we can use the new ES6 module syntax today, using Square's ES6 Module Transpiler. Remember, this syntax is not set in stone yet and could change, but that's no reason to not have a play with the new syntax today. The transpiler takes the JavaScript and transpiles it into either the CommonJS format (which is what NodeJS uses) or AMD (using RequireJS). This means you write your code using the ES6 syntax, and then run it with CommonJS, RequireJS, or similar. It's easier to show with an example. Let's make adder.js, which has a multiplier function: var multiplier = function(x) { return function(y) { return x * y; }; }; export { multiplier }; The multiplier function takes an argument and returns a function that will multiply its argument by the initial argument. So multiplier(2)(2) will return 4, and multiplier(2)(4) gives us 8. Notice the last line of the function: export { multiplier }; This uses the new ES6 syntax to export the multiplier function from this file. Now let's write a second file, app.js, and use our adder module: import { multiplier } from './adder'; var timesTwo = multiplier(2); console.log(timesTwo(4)); Again, pay particular attention to the top line: import { multiplier } from './adder'; This is how we import exported objects from modules using the ES6 syntax. To run this code, first we need to compile it. Firstly, install the ES6 transpiler: npm install -g es6-module-transpiler Now we can transpile it. For this example, as we want to run the resulting code through Node, we will tell the transpiler to use the CommonJS syntax: compile-modules app.js adder.js --to compiled --type cjs This instructs the transpiler to transpile app.js and adder.js into the compiled directory. Let's take a look at compiled/adder.js: 'use strict'; var multiplier = function(x) { return function(y) { return x * y; }; }; exports.multiplier = multiplier; Notice how it has updated the