JavaScript Modules
John Novatnack / @jnovatnack
@SquareNY
New York Front End Coders Meetup 3/26/2014
Slide 2
Slide 2 text
JavaScript Without Modules
● Code in the global namespace
● Poor code organization
● Difficult to share code between client and
server
● Painful to use third-party libraries
Three JavaScript API Modules
● CommonJS
● Asynchronous Module Definition (AMD)
● ECMAScript 6 Harmony
Slide 6
Slide 6 text
CommonJS
● CommonJS - Goal is to standardize
JavaScript APIs to improve web, desktop,
command line interoperability
● Node.JS is an implementation of CommonJS
modules for server side
Importing CommonJS Modules
var calendar = require("./calendar");
function Calendar() {
this.daysOfWeek = function(countryCode) {
if (countryCode === 'US') {
return calendar.daysOfWeekUS();
}
}};
exports.Calendar = Calendar;
Slide 9
Slide 9 text
Browserify
● Browserify bundles CommonJS modules for
shipping to the browser
$ browserify calendar.js i18n.js -o bundle.js
Slide 10
Slide 10 text
Node Libraries and Browserify
● In addition to your own modules, browserify
will bundle browser compatible node
modules
Slide 11
Slide 11 text
Importing Node Library
$ npm install console-browserify
var calendar = require("./calendar");
var log = require("console-browserify");
function Calendar() {
this.daysOfWeek = function(countryCode) {
log.debug('daysOfWeek for countryCode: ', countryCode);
// ...
}};
exports.Calendar = Calendar;
Slide 12
Slide 12 text
Asynchronous Module Definition (AMD)
● CommonJS modules have been criticized for
not being ideal for browsers
● AMD modules focus on browser modules
○ Especially asynchronous loading
RequireJS and AMD Modules
● RequireJS is a popular AMD module loader
○ Modules are loaded by the browser asynchronously
as needed
Slide 16
Slide 16 text
RequireJS Loading Example
window.displayAvailabilityCalendar = function() {
require(["i18n"], function(i18n) {
alert("Displaying calendar with day ordering " +
new i18n.Calendar().daysOfWeek("US"));
});
}
Dependencies loaded lazily Module executed after dependencies are loaded
Slide 17
Slide 17 text
ECMAScript 6 (ES 6) Modules
● ECMAScript 6 Harmony next version of
JavaScript specification
● Large number of language changes
including classes, new iterators, collections
library and modules
Importing ES6 Modules
import { daysOfWeekUS } from "./calendar";
function Calendar() {
this.daysOfWeek = function(countryCode) {
if (countryCode === 'US') {
return daysOfWeekUS();
}
}};
export { Calendar };
Slide 20
Slide 20 text
Additional ES6 Export Syntax
// Export functions, vars and ES6 classes
export function foo() { };
export var x = 10;
export class Foo { };
// Export a default for the module
export default daysOfWeekUS;
Slide 21
Slide 21 text
Additional ES6 Import Syntax
// Import the default export
import dow from "./calendar";
// Rename an import
import { daysOfWeekUS as dow } from "./calendar";
// Set module to an object
module calendar from "./calendar";
Slide 22
Slide 22 text
Use ES6 Modules Today
● ES6 module transpiler is a project started by
Square that maps ES6 modules to AMD,
CommonJS, or global modules
● Google Traceur is a general ES6 compiler
that also supports modules